header

For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

Data Types in Java

 Data Types in Java




 Data types in Java are classified into two types :

1) Primitive data types
2) Reference data types

There are 8 primitive data types in java as :

1) boolean
2) char
3) int
4) float
5) long
6) double
7) byte
8) short

1) boolean 
it is either true or false.
declaration : private boolean flag = true;
default value : false

2) byte
it contains 8-bit signed two's complement integer.
declaration : private byte b = 0;
default value : 0

3) short
it is 16-bits] signed two's complement integer.
declaration : private short s = 0;
default value : 0

4) int
it is 32-bit signed two's complement integer.
declaration : private int i = 0;
default value : 0

5) long
it is 64-bit signed two's complement integer.
declaration : private long l = 0;
default value : 0

6) char
it is single 16 bit unicode character.
declaration : private char c = 'a';
default value : '\u0000'

7) float
it is single precision 32 bit IEEE 754 floating point.
declaration : private float f = 1.8;
default value : 0.0f

8) double
it is double precision 64 bit IEEE 754 floating point.
declaration : private double d = 2.5;
default value : 0.0d

Table showing a brief of all primitive data types :

Data Type
Default value
Declaration Example
boolean
false
private Boolean b = false;
byte
0
private byte b = 0;
short
0
private short s = 2;
int
0
private int 1 = 5;
long
0
private long l = 6l;
char
\u0000
private char c = ‘a’;
float
0.0f
private float f = 3.8;
double
0.0d
private double d = 3.6;

Here is a simple program for printing values of all primitive data types : 


package Hello;

public class TestPrimitiveDataTypes {

 private static boolean b  = false;
 private static byte b1  = 1;
 private static short s  = 2;
 private static int i = 3;
 private static long l  = 2l;
 private static float f = 2.2f;
 private static double d  = 5.5d;
 private static char c  = 'a';


 public static void main(String[] args) {

 System.out.println("boolean data type : "+b);
 System.out.println("byte data type : "+b1);
 System.out.println("short data type : "+s);
 System.out.println("int data type : "+i);
 System.out.println("long data type : "+l);
 System.out.println("float data type : "+f);
 System.out.println("double data type : "+d);
 System.out.println("char data type : "+c);
 
 }
}

Output :
boolean data type : false
byte data type : 1
short data type : 2
int data type : 3
long data type : 2
float data type : 2.2
double data type : 5.5
char data type : a


Note: static is a keyword and we will discuss it in our later tutorials.

Reference Data Types

Reference data types refer to the memory address of the actual object in memory.

Reference data types can be categorized into two types:

1) Objects
2) Arrays

Object refers to a memory address. We will discuss objects in the oops concepts tutorial.
Suppose, there is a class TestRefDataTypes and an object and an instance are defined in the class.
Here object 'reftype' is a reference data type that refers to the address of instance 'a' created by class TestRefDataTypes .

package Hello;

public class TestRefDataTypes {

 static TestRefDataTypes reftype;
 static TestRefDataTypes a = new TestRefDataTypes();
  
 public static void main(String[] args) {
  reftype = a;
  System.out.println(a);
  System.out.println(reftype);
   
 }
}


Output : 
Hello.TestRefDataTypes@659e0bfd
Hello.TestRefDataTypes@659e0bfd

or if try to print hashcode values than output will be same.for printing hashcode just call hashcode method like :


System.out.println(a.hashCode());
System.out.println(reftype.hashCode());

both the hashcode will be the same


1704856573
1704856573

thus, we can see from the generated output, reftype points to the memory address of a.
if we remove the line 
  reftype = a;

from our code then the output will be :

Hello.TestRefDataTypes@659e0bfd
null

means, here reftype is null and not point to any memory address.

No comments:

Post a Comment



Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER