Introduction to Java Programming

Session on Primitive Data Types In Java

Download Java:

Link for Downloading JDK is given below
JDK 8 Download

Download Eclipse

Link for Downloading Eclipse is given below
Eclipse 2020-06 Download

Note: For any later versions of Eclipse JDK 11 or a newer version is needed

Declaring a variable with int data type in Java


public class javalabclass{
public static void main(String args[]) 
{
  //Declaring an int Variable
  int a =2147483647;
  System.out.println(a);

}
}

Declaring a variable with long data type in Java


public class javalabclass{
  public static void main(String args[]) 
  {
      //Declaring a Long Variable
      long a =2147483648L;
      System.out.println(a);
  }
} 

Declaring a variable with short data type in Java


package javaprogrammingdemo;
public class javalabclass{
  public static void main(String args[]) 
  {
      //Declaring an short Variable
      short a =32767;
      System.out.println(a);
  }
}  

Declaring a variable with float data type in Java


package javaprogrammingdemo;
public class javalabclass{
  public static void main(String args[]) 
  {
      //Declaring an float Variable
      float k = 2.3f;
      System.out.println(k);
  }
}  

Declaring a variable with double data type in Java


package javaprogrammingdemo;
public class javalabclass{
  public static void main(String args[]) 
  {
      //Declaring an double Variable
      double h = 2.34;
      System.out.println(h);
  }
}  

Declaring a variable with char data type in Java


package javaprogrammingdemo;
public class javalabclass{
  public static void main(String args[]) 
  {
      //Declaring an char Variable
      char h = 'A';
      System.out.println(h);
  }
} 

Declaring a variable with boolean data type in Java


public class javalabclass{
  public static void main(String args[]) 
  {
      //Declaring an char Variable
      boolean a = true;
      System.out.println(a);
  }
}  

Exercise - Write a program to store and print the variables of the following data Types int
long
short
byte
float
double
boolean


package javaprogrammingdemo;
public class javalabclass{
  public static void main(String args[]) 
  {
  
    int a=2;
    float b=23.f;
    long k = 23567L;
    short m = 3;
    double j = 234.556;
    boolean choice = true;
    byte h = 12;
    
    System.out.println(a);
    System.out.println(b);
    System.out.println(k);
    System.out.println(m);
    System.out.println(j);
    System.out.println(choice);
    System.out.println(h);
  }
}

Output 
2
23.0
23567
3
234.556
true
12

Print Hello World Message to the user


public class javalabclass{
  public static void main(String args[]) 
  {
       System.out.println("hello World"); 
  }
}  

Print an integer value with a message to the user


public class javalabclass{
  public static void main(String args[]) 
  {
      int a=4;
       System.out.println("The marks you have scored is "+a); 
  }
}  

Print the message Satish you have scored 4 Marks using printf function


public class javalabclass{
  public static void main(String args[]) 
  {
      int a=4;
      String s = "Satish";
       System.out.printf("%s You have scored %d marks ",s,a); 
  }
}  

Print a character within a String using printf function


public class javalabclass{
  public static void main(String args[]) 
  {
       char t = 'A';
       System.out.printf("The first character is %c",t); 
  }
}
output - The first character is A  

Code for breaking a sentence using newline character


public class javalabclass{
  public static void main(String args[]) 
  {
       System.out.printf("Satish %nshould never %nbe a professor"); 
  }
}
output
Satish 
should never 
be a professor  

Formatting float output to have a certain number of decimal places


//printing only 2 decimal places in the output
public class javalabclass{
  public static void main(String args[]) 
  {
      float k = 3.4578f;
      System.out.printf("%.2f",k);
  }
}