Showing posts with label Data types. Show all posts
Showing posts with label Data types. Show all posts

November 24, 2013

Published November 24, 2013 by

Java Data Types

Data Types: A data type or simply type is a classification identifying one of various types of data, such as whole numbers or fractional numbers.

Data Type determines:
  • the possible values for that type
  • the operations that can be done on values of that type
  • the meaning of the data
  • the way values of that type can be stored
Java is a strongly typed language because when you declare a variable, you must specify the variable’s type. Then the compiler ensures that the wrong type data is not assigned to the variable.

Java data types are mainly of two types:
  • Primitive Types
  • Reference Types
Primitive Types:
  • Primitive types are defined by the language itself. 
  • The memory location of primitive type variable contains the actual value of the variable
Reference Types:
  • Reference types are defined by classes in Java API or by classes created by the developers rather than by the language itself.
  • The memory location of reference type variable contains an address that indicates the memory location of the actual object.

Primitive Types vs. Reference Type:
Primitive Types
Reference Types
1. Primitive types are the fundamental, built in (as keyword) types
1. Reference types are constructed from primitive types either through class definition, interface definition or the use of arrays.
2. A variable of any primitive type contains a single value of the appropriate size and format.
2. A variable of a reference type holds a null reference or a reference to an instance of a class
3. Values of these types cannot be decomposed
3. A reference type can be decomposed into several values.

List of Primitive Types:
Types
Characteristics
Memory Size (byte)
Minimum Value
Maximum Value
byte
Whole numbers
1
 -128
127 
short
2
-32768
32767
int
4
-2147483648
2147483647
long
8
-9223372036854775808
9223372036854775807
float
Fractional numbers
4
1.4E-45
3.4028235E38
double
8
4.9E-324
1.7976931348623157E308
char
Any single symbol
2
'\u0000', that is 0
'\uffff' that is 65535
boolean
true/false
1



Reference Types Example:
Java classes, interfaces and arrays are reference types.
  • String
  • Date
  • And many more …
Further reading:

Read More

April 10, 2009

Published April 10, 2009 by

Minimum and Maximum Values (Range) of Java Primitive Types

The minimum and maximum values of the Java primitive types (numbers only) are as below. This will help you to declare the appropriate numeric type for your variable.


byte -128 to 127
short -32768 to 32767
int -2147483648 to 2147483647
long -9223372036854775808 to 9223372036854775807
float 1.4E-45 to 3.4028235E38
double 4.9E-324 to 1.7976931348623157E308


You can get the result from the following Java code:

System.out.println("byte "+Byte.MIN_VALUE+" to "+Byte.MAX_VALUE);
System.out.println("short "+Short.MIN_VALUE+" to "+Short.MAX_VALUE);
System.out.println("int "+Integer.MIN_VALUE+" to "+Integer.MAX_VALUE);
System.out.println("long "+Long.MIN_VALUE+" to "+Long.MAX_VALUE);
System.out.println("float "+Float.MIN_VALUE+" to "+Float.MAX_VALUE);
System.out.println("double "+Double.MIN_VALUE+" to "+Double.MAX_VALUE);

Here for float, 1.4E-45 means 1.4 x 10-45 

The minimum & maximum value of float & double in plain string are given in the table below:

flaot minimum value
0.000000000000000000000000000000000000000000001401298464324817070923729583 28991613128026194187651577175706828388979108268586060148663818836212158203
125
float maximum value
340282346638528859811704183484516925440
double minimum value
0.000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000494065645841246544176568792868221372365059802 61432476442558568250067550727020875186529983636163599237979656469544571773 09266567103559397963987747960107818781263007131903114045278458171678489821 03688718636056998730723050006387409153564984387312473397273169615140031715 38539807412623856559117102665855668676818703956031062493194527159149245532 93054565444011274801297099995419319894090804165633245247571478690147267801 59355238611550134803526493472019379026810710749170333222684475333572083243 19360923828934583680601060115061698097530783422773183292479049825247307763 75927247874656084778203734469699533647017972677717585125660551199131504891 10145103786273816725095583738973359899366480994116420570263709027924276754 4565229087538682506419718265533447265625
double maximum value
17976931348623157081452742373170435679807056752584499659891747680315726078 00285387605895586327668781715404589535143824642343213268894641827684675467 03537516986049910576551282076245490090389328944075868508455133942304583236 90322294816580855933212334827479782620414472316873817718091929988125040402 6184124858368
Read More