sads

Monday, 19 December 2016

Basics of Java

1. Program to Display String Literal

class  Display 
{
 public static void main( String args[ ] )
 {
  System.out.println("Let  us learn JAVA." );
 }
}


2. Program to Explain Different types of Comments in Java Program  
/*
This is a simple Java program.
Call this file "Comments.java".
*/
class Comments
{
 // Your program begins with a call to main().
 public static void main( String args[ ] )
 {
  System.out.println("BIIT Computer Education.");
 }
}

3. Program to Find Area of Circle

class  AreaCircle

 public static void main( String args[ ] )
 {
  double rad;
  final double PI = 3.14159;

   rad = 10.0;

   double area = PI * rad * rad;
  System.out.print( "\n Area of Circle is = " + area );
 }
}


4. Program to explain Dynamic Intialization of Variable 

class  DynInit
{
 public static void main( String args[ ] )
 {
  double a = 3.0, b = 4.0;

   // c is dynamically initialized
  double c = Math.sqrt(a * a + b * b);

   System.out.println("Hypotenuse is " + c);
 }
}

5. Program to declare variable at end of class

class  DeclareAtEnd
{
 public static void main( String args[ ] )
 {
  msg = "Let's learn JAVA !";
        System.out.println("Message : " + msg);
 }

 static String msg;
}


6.Program to declare static variable at end of class.

class  DeclareAtEnd1
{
 public static void main( String args[ ] )
 {
  DeclareAtEnd1 ob = new DeclareAtEnd1();
  ob.msg = "Let's learn JAVA !";
        System.out.println("Message : " + ob.msg);
 }

 String msg;
}


7. Program to declare and use character variables.

class  CharDemo
{
 public static void main(String args[ ]) 
 {
  char  ch1, ch2;
  ch1 = 88;   // code for X
  ch2 = 'Y';
  System.out.print("ch1 and ch2 : ");
  System.out.println(ch1 + " " + ch2);

   ch1++;    // increment ch1
  System.out.println("ch1 :  " + ch1);
 }
}


8. Program to declare and use Boolean variable 

class BoolTest
{
 public static void main(String args[ ]) 
 {
  boolean b;
  b = false;
  System.out.println("b is " + b);

   b = true;
  System.out.println("b is " + b);

   if(b) 
   System.out.println("This is executed.");

   b = false;
  if(b) 
   System.out.println("This is not executed.");

   System.out.println("10 > 9 is " + (10 > 9));
 }
}



9. Program to use Integer( feature of using underscore is available in jdk 7) 

class J7_Literal2
{
 public static void main( String args[ ] )
 {
  long dec = 1234_5678_9876_5432L;
  int hex = 0xE_23D5_8C_7;
  int bin = 0b1110_0010001111010101_10001100_0111;
  
  System.out.println("Dec number : " + dec);
  System.out.println("Hex number : " + hex);
  System.out.println("Bin number : " + bin);
 }
}

10. Program to explain Lifetime of variable 

class LifeTime1
{
 public static void main(String args[ ]) 
 {
  int i;
  for(i = 1; i <= 5; i++) 
  {
   increment();
  }
 }

 private static void increment()
 {
  int  x = 10;
  x++;
  System.out.println(" " + x );
 }
}



11. Program to create and use Constant 

class Constant1
{
 static final int NUMBER_OF_MONTHS = 12;
 static final double PI = (double) 22 / 7;

  public static void main( String args[] )
 {
  System.out.println("NUMBER_OF_MONTHS : " + NUMBER_OF_MONTHS );
  System.out.println("PI : " + PI );
 }
}



12. Program to create public Constant and use it 

class MyConstant
{
 public static final int NUMBER_OF_MONTHS = 12;
 public static final double PI = (double) 22 / 7;
}

class Constant2
{
 public static void main( String args[ ] )
 {
  System.out.println("NUMBER_OF_MONTHS : " + MyConstant.NUMBER_OF_MONTHS );
  System.out.println("PI : " + MyConstant.PI );
 }
}



13. Program to Read data From keyboard using Scanner Class

class Input1
{
 public static void main( String args[ ] )
 {
  Scanner sc = new Scanner( System.in );
  int n;
  
  System.out.print( "Enter a Number : ");
  n = sc.nextInt();

   System.out.println( "The given number : " + n );
 }
}


14. Program to read data from keyboard using Console class.

import java.io.*;

class Input2
{
 public static void main( String args[ ] )
 {
  Console cn = System.console();
  int n;
  
  System.out.print( "Enter a Number : ");
  n = Integer.parseInt( cn.readLine() );

   System.out.println( "The given number : " + n );
 }
}

11 comments:

  1. As a biggner in java programming its really help to me. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    best java training in chennai |
    Core java training in chennai

    ReplyDelete
  2. I am really very happy to find this particular site. I just wanted to say thank you for this huge read!! I absolutely enjoying every petite bit of it and I have you bookmarked to test out new substance you post.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

    ReplyDelete
  3. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    best java training in coimbatore
    php training in coimbatore

    ReplyDelete
  4. Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part I care for such info a lot. I was seeking this particular information for a very long time. Thank you and good luck.
    北美Java代写

    ReplyDelete
  5. Good blog
    For data science training in Bangalore Visit:
    Data Science training in bangalore

    ReplyDelete
  6. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.thanks lot!!

    android training in chennai

    android online training in chennai

    android training in bangalore

    android training in hyderabad

    android Training in coimbatore

    android training

    android online training

    ReplyDelete

  7. This post is so interactive and informative.keep update more information...
    RPA Training in Velachery
    RPA Training in Chennai

    ReplyDelete
  8. This post is so interactive and informative.keep update more information...
    German Classes in Tambaram
    German Classes in chennai

    ReplyDelete