Primitive, Custom/reference Data Types, Integer, Floating-Point, Character and String literal, Escape sequence in java, decimal to hexaDecimal and binary conversion program, Implicit casting/promotion of primitive Data type


Contents of page :
  • Primitive Data Types in java >
  • Custom/reference Data Types in java >



  • Literals in java >
  • Integer Literals >
  • Program - How to convert >
  • decimal to hexaDecimal (using Java API)
  • decimal to binary (using Java API)
  • Floating-Point Literals >
  • Character and String Literals >

  • Escape sequence in java >

  • Implicit casting/promotion of primitive Data type in java >



Primitive Data Types >
There are 8 primitive data types in Java. All Primitive data types are keyword in java language. Let us now look into detail about the eight primitive data types.

Size
Minimum value
Maximum value
Default value
When to use
Example (Declaration of data type)
byte
8-bit
signed two's complement integer.
-128 (-27)
127 (27 -1)
0
The byte data type can be handy in saving memory in large arrays.
      
byte b = -128 ;
byte b1 = 127;
                      
         
short
16-bit
signed two's complement integer.
-32,768 (-215)
32,767
(215 -1)
0
The short data type can be handy in saving memory in large arrays.
short s = -32768;
short s1 =32767;
short s_ =32_767  ;

Note : Java 7 allows you to use _ between digits to improve readability of code.
int
32-bit
signed two's complement integer.
-2,147,483, 648
(-231)
2,147,483, 647
(231 -1)
0
int is used often for storing integer values (if memory is not a concern)
int i = -2147483648;
int i1 = 2147483647;
int i_ = 2_147_483_647;
         
long
64-bit
signed two's complement integer.
-9,223,372, 036,854, 775,808
(-263)
9,223,372, 036,854,  775,807  
(263 -1)
0L
long is used when a wider range than int is required.
long l = -9223372036854775808L;
long l1 = 9223372036854775807L;
long l_ = 9_223_372_036_854_775_807L;
float
single- precision
32-bit IEEE 754 floating point.


0.0f
The float data type can be handy in saving memory in large arrays.

Float data type is never used for precise values such as currency.
float f = -1.1f;
float f1 = 1.1f;
double
single- precision
64-bit IEEE 754 floating point.


0.0d
double is generally used as the data type for large decimal values.

Double data type should never be used for precise values such as currency.
double d = -2.3d;
double d1 = 2.3d;
boolean
1-bit


false
boolean is used when value can be either true or false
boolean boo=false;
char
single 16-bit Unicode character
'\u0000'  or
0
'\uffff' or 65,535

char is used to store character.
char c='a';






Custom/reference Data Types in java >

  • Custom data types are created by defining and calling constructors of the class.
Example - We may create employee class and define its constructor.
class Employee {
   private Integer id;
  
   /** Employee constructor*/
   public Employee(Integer id) {
          this.id = id;
   }
}
/** Main class*/
public class MyClass {
    public static void main(String...a) {
           Employee emp; //emp is pointing to null
           emp=new Employee(1); //emp is pointing to object
    }
}
emp is reference variable of Employee type.

> emp can refer to any subClass of Employee as well
emp=new SubEmployee();
  Where SubEmployee is subClass of Employee
class SubEmployee extends Employee{
   SubEmployee(){
          super(1);
   }
}


Literals in java >

  • primitive types are special data types in java.
  • primitive types are created without using new keyword.
  • primitive types are not objects.
  • A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation
Example -
byte b = -128 ;
short s = -32768;
int i = -2147483648;



Integer Literals >

Integer literal is of type long if it ends with the L or l, otherwise it is int type.
int literals can be used to create values of the byte, short, int, and long types.

Integer literals can be expressed by these number systems:
  • Decimal (Base 10) -  digits between 0 through 9
  • Hexadecimal (Base 16) - digits between 0 to 9  & letters between  A to F
  • Binary (Base 2) in java 7 - digits 0 and 1.
Example -
            int decimalInt = 26; //decimal
          int hexaDecimalInt = 0x1c; //hexadecimal
          int binaryInt = 0b11100; //binary


Program - How to convert >
  • decimal to hexaDecimal (using Java API)
  • decimal to binary (using Java API)
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Conversions {
   public static void main(String[] args) {
         
          int i =28; //decimal
          //decimal to hexaDecimal conversion
          String hexaDecimalInt  = Integer.toHexString(i);
          System.out.println("hexaDecimal > " + hexaDecimalInt); //1c
          //decimal to binary conversion
          String binaryInt  = Integer.toBinaryString(i);
          System.out.println("binary   > " + binaryInt ); //11100
   }
}
/*OUTPUT
hexaDecimal  > 1c
binary   > 11100
*/

Floating-Point Literals
A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.
A floating-point literal is double by default (optionally, we may use D or d)
If floating-point literal is float if and only if it ends with F or f.
The floating-point types can be expressed using scientific notation (E or e)

double d = 131.4;  //floating-point literal 131.4 is double by default
float f  = 131.4f; //floating-point literal 131.4f is float
// using d in scientific notation (Note: d is equal to d1)
double d1 = 1.314e2;  //1.314e2 = 1.314 * 100


Character and String Literals >

char and String type literals contain any Unicode (UTF-16) characters.
If editor and file system allow it, you can use such characters directly in your code.

We must always use >
  • 'single quotes' for char literals and
  • "double quotes" for String literals.

Escape sequence in java >
Escape sequence
Description
\n
new line
\t
tab
\b
backspace


\'
single quote
\"
double quote


\\
backslash
\s
space


\r
carriage return
\f
formfeed




Implicit casting/promotion of primitive Data type in java >
It’s very important from ocjp exam point of view.

Diagram of Implicit casting/promotion of primitive Data type in java >


Tabular form Implicit casting/promotion of primitive Data type in java >
Primitive Data type
Can be implicitly casted/promoted to
byte
short, int, long, float, double
short
        int, long, float, double
char
        int, long, float, double
int
             long, float, double
long
                     float, double
float
                             double
boolean
-

boolean cannot be casted implicitly or explicitly to any other datatype.


Now, let’s create programs to understand Implicit Data type casting/ promotion of primitive data types in java with method overloading in java >

Program 1.1 -
In the program, 2 is a int, 2 can be casted to double as well, but rather than casting compiler will call method with int as argument.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
   static void m(double x) {
          System.out.println("double");
   }
   static void m(int x) {
          System.out.println("int");
   }
  
   public static void main(String args[]) {
          m(2);
   }
}
/*OUTPUT
int
*/


Program 1.2 -
In the program, 2 is a int, 2 can be casted to double and float as well, but rather than casting to double compiler will cast 2 to float and call method with float as argument.

Q. But why in float, why not in double?
A. Because float consumes less memory than double.
public class MyClass {
   static void m(double x) {
          System.out.println("double");
   }
   static void m(float x) {
          System.out.println("float");
   }
  
   public static void main(String args[]) {
          m(2);
   }
}
/*OUTPUT
float
*/


Program 1.3 -
In the program, 2 is a int, 2 can be casted to double but cannot be casted to short, compiler will cast 2 to double and call method with double as argument.
public class MyClass {
   static void m(double x) {
          System.out.println("double");
   }
   static void m(short x) {
          System.out.println("short");
   }
  
   public static void main(String args[]) {
          m(2);
   }
}
/*OUTPUT
double
*/


Program 1.3 -
In the program, 2 is a int, 2 can’t be casted to either short or byte, so we will face compilation error
public class MyClass {
   static void m(byte x) {
          System.out.println("double");
   }
   static void m(short x) {
          System.out.println("short");
   }
  
   public static void main(String args[]) {
          m(2);  //compilation error
   }
}
/*OUTPUT
*/



Data types are not demoted Implicitly, though they may be demoted explicitly, which may cause data loss.

Example Program 2 - If we want to demote double to int, we will have to add explicit cast. In explicit cast we lost decimal value of 2.2 and are left with 2
public class MyClass {
   public static void main(String args[]) {    
          double d=2.2;
          int i=(int) d;
          System.out.println(i);
   }
}
/*OUTPUT
2
*/





RELATED LINKS>

Arithmetic, Unary, Equality, Relational, Conditional, Bitwise, Bit Shift, Assignment, instanceof operators in detail with programs.


Control Flow Statements = Decision making - if, else, switch | Looping - for, Infinite, Enhanced for loop, while, do-while | Branching - break, Labeled break, continue, Labeled continue, return, goTo


No comments:

Post a Comment