Crystal Academy
  • Home
  • Android
  • ASP.NET
  • C
  • C++
  • DBMS
  • HTML
  • iOS
  • Java
    • Introduction to Java
      • Features of Java
      • Data Types in Java
      • Operators in Java
      • Type Conversions in Java
      • Byte Order in Java
      • Conditional Statement
      • Iterative Statement
    • Object Oriented Concepts
      • Class And Object
      • Function Overloading/Polymorphism
      • Passing Objects in Argument
      • Static Variable and Static Method
      • Finalize Method
      • Constructors in Java
      • Initializer Block
      • this keyword
      • Inheritance
      • Constructor implementation in Inheritance
      • Method Overriding in Java
      • Final Keyword
      • Abstract Class and Abstract Method
      • Interface
      • Package
      • String Class
      • StringBuffer Class
      • StringBuilder Class
      • Wrapper Classes
      • Exception Handling
      • Creating custom exception classes
    • Multithreading
      • Thread Class and Methods
      • Thread synchronization
      • Thread intercommunication
    • Lifecycle of Applet
    • Graphics Class
  • JavaScript
  • OS
  • PHP
  • VB.Net
  • Contact Us
Top Posts
Byte Order in Java: Big-endian and Little-endian
Classes and Objects in Java
Conditional Statements
An Introduction to Unix
An Introduction to C Programming
An Introduction to C++
An Introduction to Android
An Introduction to Operating Systems(OS)
An Introduction to DBMS
An Introduction To ASP.NET

Crystal Academy

  • Home
  • Android
  • ASP.NET
  • C
  • C++
  • DBMS
  • HTML
  • iOS
  • Java
    • Introduction to Java
      • Features of Java
      • Data Types in Java
      • Operators in Java
      • Type Conversions in Java
      • Byte Order in Java
      • Conditional Statement
      • Iterative Statement
    • Object Oriented Concepts
      • Class And Object
      • Function Overloading/Polymorphism
      • Passing Objects in Argument
      • Static Variable and Static Method
      • Finalize Method
      • Constructors in Java
      • Initializer Block
      • this keyword
      • Inheritance
      • Constructor implementation in Inheritance
      • Method Overriding in Java
      • Final Keyword
      • Abstract Class and Abstract Method
      • Interface
      • Package
      • String Class
      • StringBuffer Class
      • StringBuilder Class
      • Wrapper Classes
      • Exception Handling
      • Creating custom exception classes
    • Multithreading
      • Thread Class and Methods
      • Thread synchronization
      • Thread intercommunication
    • Lifecycle of Applet
    • Graphics Class
  • JavaScript
  • OS
  • PHP
  • VB.Net
  • Contact Us

Data Types in Java

Data Types in Java
Data Types in Java

Data Types in Java

written by Sanjay Nair

Data Types in Java

  • There are majorly two types of languages.
  • First one is Statically typed language where each variable and expression type is already known at compile time.
  • Once a variable is declared to be of a certain data type, it cannot hold values of other data types.
  • Java is statically typed and also a strongly typed language because in Java, each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.

 

Java has two categories of data:

  • Primitive data (e.g., number, character)
  • Object data (programmer created types)

Primitive data

  • Primitive data are only single values; they have no special capabilities.
  • There are 8 primitive data types

 

Data TypeSize(bytes)
boolean1
int4
short2
byte1
long8
float4
double8
char2

 

 

boolean

  • boolean data type represents only one bit of information either true or false .
  • Values of type boolean are not converted implicitly or explicitly (with casts) to any other type.

byte

  • The byte data type is an 8-bit signed two’s complement integer.The byte data type is useful for saving memory in large arrays.
  • Size: 8-bit
  • Value: -128 to 127

short

  • The short data type is a 16-bit signed two’s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
  • Size: 16 bit
  • Value: -32,768 to 32,767 (inclusive)

int

  • It is a 32-bit signed two’s complement integer.
  • Size: 32 bit
  • Value: -231 to 231-1

long:

  • The long data type is a 64-bit two’s complement integer.
  • Size: 64 bit
  • Value: -263 to 263-1.

Floating point Numbers : float and double

float

  • The float data type is a single-precision 32-bit IEEE 754 floating point. Use a float (instead of double) if you need to save memory in large arrays of floating point numbers.
  • Size: 32 bits
  • Suffix : F/f Example: 9.8f

double:

  • The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice.

char

  • The char data type is a single 16-bit Unicode character. A char is a single character.

A Sample Java Program to demonstrate the primitive data types

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class DataTypes
{
public static void main(String args[])
{
 
// declaring character
 
char a = 'G';
 
// Integer data type is generally
 
// used for numeric values
 
int i=89;
 
// use byte and short if memory is a constraint
 
byte b = 4;
 
// this will give error as number is
 
// larger than byte range
 
// byte b1 = 7888888955;
 
 
short s = 56;
 
// this will give error as number is
 
// larger than short range
 
// short s1 = 87878787878;
 
 
// by default fraction value is double in java
 
double d = 4.355453532;
 
// for float use 'f' as suffix
 
float f = 4.7333434f;
 
System.out.println("char: " + a);
System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}

 

Output

char: G

integer: 89

byte: 4

short: 56

float: 4.7333436

double: 4.355453532

 

 

December 25, 2017 0 comment
2 Facebook Twitter Google + Pinterest

Recent Posts

  • Byte Order in Java: Big-endian and Little-endian

  • Classes and Objects in Java

  • Conditional Statements

  • An Introduction to Unix

  • An Introduction to C Programming

Categories

  • Android
  • ASP.NET
  • C
  • C++
  • DBMS
  • English
  • HTML
  • Java
    • Byte Order in Java
    • Class And Object
    • Conditional Statement
    • Data Types in Java
    • Features of Java
    • Iterative Statement
    • Type Conversions in Java
  • OS
  • UNIX
  • VB.Net
  • Facebook
  • Twitter
  • Email

@2017 - Crystal Virtual Academy. All Right Reserved. Developed by Precise Developers - N+1 IT Solutions

Overall rating is 4.8 out of 5.0 for Crystal Virtual Academy by 500+ Visitors.


Back To Top