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

28 September 2010

Literals

boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;
>> You may have noticed that the new keyword isn't used when  initializing a variable of a primitive type.
>> Primitive types are special data  types built into the language; they are not objects created from a class.
>> A  literal is the source code representation of a fixed value;
>> literals  are represented directly in your code without requiring computation
The integral types (byte, short,  int, and long) can be expressed using decimal, octal,  or hexadecimal number systems. 
int decVal = 26;  // The number 26, in decimal
int octVal = 032;  // The number 26, in octal
int hexVal = 0x1a; // The number 26, in hexadecimal
 
 
double d1 = 123.4;
double d2 = 1.234e2;  // same value as d1, but in scientific notation
float f1  = 123.4f;
 
The Java programming language also supports a few special escape sequences  for char and String literals:
\b  (backspace),
\t (tab),
\n (line feed),
\f  (form feed),
\r (carriage return),
\" (double quote), 
\' (single quote),
and \\ (backslash).
 
  
>> There's also a special null literal that can be used as a value  for any reference type. There's also a special null literal that can be used as a value  for any reference type. null may be assigned to any variable,  except variables of primitive types. may be assigned to any variable,  except variables of primitive types.
>> Therefore,  null is often used in programs as a marker to indicate that some  object is unavailable.
>> There's also a special kind of literal called a class  literal, formed by taking a type name and appending ".class";  for example, String.class. This refers to the object (of type  Class) that represents the type itself.  

Default Values of Premitive type

Default Values

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.
The following chart summarizes the default values for the above data types.



Data Type
Default Value (for fields)
byte
0
short
0
int
0
long
0L
float
0.0f
double
0.0d
char
'\u0000'
String (or any object)
null
boolean
false


Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.

23 September 2010

A program to print limits of the primitive types

public class PrintLimits
{

       public PrintLimits(){
       }

       public static void main(String args[]) {

              System.out.println("Min byte value = " + Byte.MIN_VALUE);
              System.out.println("Max byte value = " + Byte.MAX_VALUE);
              System.out.println("Min short value = " + Short.MIN_VALUE);
              System.out.println("Max short value = " + Short.MAX_VALUE);
              System.out.println("Min int value = " + Integer.MIN_VALUE);
              System.out.println("Max int value = " + Integer.MAX_VALUE);
              System.out.println("Min float value = " + Float.MIN_VALUE);
              System.out.println("Max float value = " + Float.MAX_VALUE);
              System.out.println("Min double value = " + Double.MIN_VALUE);
              System.out.println("Max double value = " + Double.MAX_VALUE);
       }
}