Data Types

Data Types, Variables & Operators in Java

 "Data type is nothing but the type of the data. Each Variable in Java must have certain
         type associated with it which tells us what kind of data a variable can store."

 

Type

Contains

Default

Size

boolean

true or false

false

1 bit

char

Unicode Character

u0000

16 bits

byte

Signed Integer

0

8 bits

short

Signed Integer

0

16 bits

int

Signed Integer

0

32 bits

long

Signed Integer

0

64 bits

float

Floating Number

0.0

32 bit

double

Floating Number

0.0

64 bit

 

NON-PRIMITIVE DATATYPES

       The data types that are derived from primary data types are known as non-Primitive data types.
These datatypes are used to store group of values.The non-primitive data types are Arrays, Structure,
Union, linked list, Stacks, Queue etc

 

EXERCISE: 

1.   Converting String to int
2.   Converting int to String
3.   Converting float to string
4.   Checking the string starts with characters "ab"
5.   Checking the string ends with characters "de"
6.  String concatenation: to combine two strings “Tech” and “Aviator”
7. 
Arithmetic Calculations: To add values of variable a & b and store the result in C
8. 
Modulus operator % : To get the reminder of interger a ,b and c
9. 
Applying the modulus operator, %, to floating-point values 
10.
AND operator to validate the values of a and b
11.
Java program to find odd or even numbers
12.
Java program to convert Fahrenheit to Celsius

 

 

1) Converting String to int : Convert from String 

Example:
public class TechAviator {
public static void main(String[] arg) {
    System.out.println(Integer.parseInt("100"));   
  }
}
 2) Convert  int to String 

Example:
public class TechAviator {
   public static void main(String[] arg) {
    int b = 12;
    System.out.println(String.valueOf(b));   
   }
}
 
3) Converting float to String 

Example:
public class TechAviator {
   public static void main(String[] arg) {
    float f = 12.13145F;
    System.out.println(String.valueOf(f));   
   }
}
4) Checking the String Starts with characters  “ab”

Example:
public class TechAviator{
   public static void main(String[] arg) {
        String string1 = "abcde";
    if(string1.startsWith("ab")) {
      System.out.println("starts with ab");
    }
  }
}
 
5) Checking the String ends with characters  “de”

Example:
public class TechAviator {
   public static void main(String[] arg) {
        String string1 = "abcde";
    if(string1.endsWith("de")) {
      System.out.println("ends with de");
    }
  }
}
 
6) String concatenation: to combine two strings “Tech” and “Aviator”   
Example:
public class TechAviator {
  public static voidmain(String[] arg) {
    String myString = "Tech" + "Aviator";
      System.out.println(myString);
  }
}
7) Arithmetic Calculations: To add values of variable a & b and store the result in c 

Example
public class TechAviator{
  public static void  main(String[] argv){
     int a = 1;
     int b = 2;
     int c = 0;
          c = a + b;
          System.out.println(c);
  }
}
8)  Modulus operator % : To get the reminder of interger a ,b and c 

Example:
public class TechAviator {
   public static void  main(String[] argv) {
    int a = 3 % 2;
    int b = 11 % 3;
    int c = 7 % -3;
     System.out.println(a);
    System.out.println(b);
    System.out.println(c);
  }
 }
 9) Applying the modulus operator, %, to floating-point values 

Example:
public class TechAviator {
   public static void  main(String[] arg){
     double a = 2.4;
     double b = 0.2;  
     System.out.println( a % b );
  }
 
}
10)  AND operator to validate the values of a and b

Example:
public class TechAviator {
   public static void  main(String[] arg) {
    int a = 0;
    int b = 1;
     if (a == 0 && b == 1) {
      System.out.println("here");
    } else {
      System.out.println("there");
    }
  }
}
 
11) Java program to find odd or even number

Example:
import  java.util.Scanner;
 class OddOrEven
{
   public static void  main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
       if ( x % 2 == 0 )                                           
         System.out.println("You entered an even number.");
      else
         System.out.println("You entered an odd number.");
   }
}
12)  Java program to convert Fahrenheit to Celsius  
Example:
import  java.util.*; 
class FahrenheitToCelsius  {
  public static void  main(String[] args) {
    float temperatue;
    Scanner in = new Scanner(System.in);       
    System.out.println("Enter temperatue in Fahrenheit");
    temperatue = in.nextInt(); 
    temperatue = ((temperatue - 32)*5)/9; 
    System.out.println("Temperatue in Celsius = " + temperatue);
  }
}