Skip to content

Latest commit

 

History

History
161 lines (117 loc) · 4.61 KB

0.2.1_data_types.md

File metadata and controls

161 lines (117 loc) · 4.61 KB

1) Data types in Java

Java has two types of data types- Primitive and Non-Primitive.

a) Primitive data types

In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double.

  • Keywords

b) Non-Primitive data types- String, Array, Objects.

  • Derived from primitive
  • Classes in java , inbuilt methods
  • Objects – own methods

1.1) Primitive data types-**

In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double.

  • byte, short, int and long data types are used for storing whole numbers.

  • float and double are used for fractional numbers.

  • char is used for storing characters(letters).

  • boolean data type is used for variables that holds either true or false.

1.1.1) byte-

  • This can hold whole number between -128 and 127.
  • Size in memory- 1 byte. ==> 8 bits => 1 and 0
  • Default value- 0.

1.1.2) short-

  • This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767. ,
  • Default size of this data type: 2 byte
  • Default value- 0

1.1.3) int-

  • int is greater than short it has a wider range: -2,147,483,648 to 2,147,483,647
  • Default size: 4 byte ==> 32 bits
  • Default value: 0

1.1.4 long-

  • long is bigger than int. It has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • size: 8 bytes
  • Default value: 0

1.1.5) float- The float data type is a single-precision 32-bit IEEE 754 floating point. float is used to store numbers with decimal values. float contains values upto 7 decimal points. Default value is 0.0. float number should end with letter 'f'.

Example-

public class floatExample
         {
             public static void main(String[] args)
                  {
                       float myNum = 5.75f; // without store compiler may store as double, requires more memory, 15 decimal point, memory waste
                       System.out.println(myNum);
                  }
         }

Output- 5.75

A floating point number can also be a scientific number with an "e" to indicate the power of 10. Example-

 public class floatExample
      {
         public static void main(String[] args) 
            {
                  float f1 = 35e3f;
                  double d1 = 12E4d;
                  System.out.println(f1);
                  System.out.println(d1);  
             }
       }

Output- 35000.0 120000.0 `

https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3

1.1.6) Double- The double data type is a double-precision 64-bit IEEE 754 floating point. double is used to store numbers with decimal values. For decimal values, this data type is generally the default choice. double contains values upto 15 decimal points. Default value is 0.0. double number should end with letter 'd'. Example-

          `  public class doubleExample
                 {
                     public static void main(String[] args) 
                       {
                           double myNum = 19.99d;
                           System.out.println(myNum);  
                       }
                 }`

Output- 19.99

- > we cannot check size of double and float directly to compare. Can write our own methods etc. to do so.

A double can also be a scientific number like float with an "e" to indicate the power of 10 as mentioned float section above. Example-

 public class doubleExample
          {
             public static void main(String[] args) 
                {
                      float f1 = 35e3f;
                      double d1 = 12E4d;
                      System.out.println(f1);
                      System.out.println(d1);  
                 }
           }

Output- 35000.0 120000.0

System – package , memory optimization handled by garbage collector.

Discussing float and double data types will be out of scope of this document. Please refer following link for further details- https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3

1.1.7) boolean-

This data type holds only true or false. 1 byte. Example-

class BooleanExample
             {
                 public static void main(String[] args) 
                       {    	
    	                 boolean b = false;
    	                 System.out.println(b);
                       }
             }

Output- false

1.1.8 char- This data type holds characters. Char values should be surrounded by single quotes. Size- 2 bytes. Example-

char ch= 'b';

Check assigment here