Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Data Type in Java

data type in java

Table of Contents

  • Data Type in Java
    • 1) Pri-mitive Data type
    • 2) Non-Primitive Data type in Java (Reference Data type)
      • Identifiers in Java

Data Type in Java

Data Type in Java

Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.

In java, data types are classified into two categories :

  1. Pri-mitive Data type
  2. Non-Pri-mitive Data type

1) Pri-mitive Data type

A pri-mitive data type can be of eight types :

Pri-mitive Data types
charbooleanbyteshortintlongfloatdouble

Once a pri-mitive data type has been declared its type can never change, although in most cases its value can change. These eight pri-mitive type can be put into four groups

Integer

This group includes byte, short, int, long

byte : It is 1 byte(8-bits) integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;

short : It is 2 bytes(16-bits) integer data type. Value range from -32768 to 32767. Default value zero. example: short s=11;

int : It is 4 bytes(32-bits) integer data type. Value range from -2147483648 to 2147483647. Default value zero. example: int i=10;

long : It is 8 bytes(64-bits) integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;

Example:

Lets create an example in which we work with integer type data and we can get idea how to use datatype in the java program.


package corejava;

public class Demo{      
    
    public static void main(String[] args) {
    	// byte type
    	byte b = 20;
    	System.out.println("b= "+b);
    	
    	// short type
    	short s = 20;
    	System.out.println("s= "+s);
    	
    	// int type
    	int i = 20;
    	System.out.println("i= "+i);
    	
    	// long type
    	long l = 20;
    	System.out.println("l= "+l);
    	
   
    }  
}  
	

b= 20 s= 20 i= 20 l= 20

Floating-Point Number

This group includes float, double

float : It is 4 bytes(32-bits) float data type. Default value 0.0f. example: float ff=10.3f;

double : It is 8 bytes(64-bits) float data type. Default value 0.0d. example: double db=11.123;

Example:

In this example, we used floating point type and declared variables to hold floating values. Floating type is useful to store decimal point values.


public class Demo{      
    
    public static void main(String[] args) {
    	// float type
    	float f = 20.25f;
    	System.out.println("f= "+f);
    	
    	// double type
    	double d = 20.25;
    	System.out.println("d= "+d);
   
    }  
}  
	

f= 20.25 d= 20.25

Characters

This group represent char, which represent symbols in a character set, like letters and numbers.

char : It is 2 bytes(16-bits) unsigned unicode character. Range 0 to 65,535. example: char c='a';

Char Type Example

Char type in Java uses 2 bytes to unicode characters. Since it works with unicode then we can store alphabet character, currency character or other characters that are comes under the unicode set.


public class Demo {

    public static void main(String[] args) {


        char ch = 'S';
        System.out.println(ch);
        
        char ch2 = '&';
        System.out.println(ch2);
        
        char ch3 = '$';
        System.out.println(ch3);

    }

}
    

S & $

Boolean

This group represent boolean, which is a special type for representing true/false values. They are defined constant of the language. example: boolean b=true;

Boolean Type Example

Boolean type in Java works with two values only either true or false. It is mostly use in conditional expressions to perform conditional based programming.


public class Demo {

    public static void main(String[] args) {

        boolean t = true;
        System.out.println(t);
        
        boolean f = false;
        System.out.println(f);

    }

}
    

true false

2) Non-Primitive Data type in Java (Reference Data type)

A reference data type is use to refer to an object. A reference variable is declare to be of specific and that type can never be change.

For example: String str, here str is a reference variable of type String. String is a class in Java. We will talk a lot more about reference data type later in Classes and Object lesson.

Reference type are use to hold reference of an object. Object can be instance of any class or entity.

In object oriented system, we deal with object that stores properties. To refer those objects we use reference types.

We will talk a lot more about reference data type later in Classes and Object lesson.


Identifiers in Java

All Java components require names. Name used for classes, methods, interfaces and variables are called Identifier. Identifier must follow some rules. Here are the rules:

  • All identifiers must start with either a letter( a to z or A to Z ) or currency character($) or an underscore.
  • After the first character, an identifier can have any combination of characters.
  • Java keywords cannot be used as an identifier.
  • Identifiers in Java are case sensitive, foo and Foo are two different identifiers.

Some valid identifiers are: int a, class Car, float amount etc.


Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.