Skip to main content

Literals

Literals

Literal is the value that we pass in the variables. In other words, Literals are syntactic representation of boolean,numeric,character and string type. These values can be fixed or constant and are stored in the variable.

Generally literal are categorized into two types i.e. Numeric Literals and Non Numeric Literals.

Integer Literal : They includes - int, short, long, byte.

These are the value without any decimal.
Int is default type of integer. if any value is out of range in integer literal then compiler will consider that value as int and generates an error.
To represent any value in long L/l will be used at the end.
There are 4 integer Literals type i.e. Decimal, Octal, Hexadecimal, Binary(added in java 7)
  •  Decimal Literals has base 10 i.e from 0 to 9.
         They must not be started with 0.
Example:
         int a = 111;
         it will print 111

  • Octal Literals has base 8 i.e. from 0 to 7.
         They must be started with 0.
Example:
         int a = 0111;
         i.e. 1*82+1*81+1*80 = = 64+8+1 = 73
         
  • Hexadecimal Literals has base 16 from 0 to 9 and a to f.
         They must start with 0x / 0X.
 Example:
         int a = 0x111;
         i.e. 1*162+1*161+1*160 = = 256+16+1 = 273

  • Binary Literal has base 2 i.e. 0,1.
         They must start with 0b / 0B.
Example:
         int a = 0b111;
         i.e. 1*22+1*21+1*20 = 4+2+1 = 7


Floating Point Literal : They includes - float and double.
Default value of Floating Literals is double.
For assigning any value to float f / F will be used at the end.
Floating Point Literals stores values in two parts i.e. values and exponential.
Its representation will not allows with Octal and Binary.

Floating Point Notation:
  • Standard Notation - for eg. 456.19
  • Exponential/Scientific Notation - 45619E-2
  • Hexadecimal Notation - 0x45619p0


Character Literals : It include - char.

There are five ways to assign values - 
  • Single character with single quote.
  • ASCII values
  • Unicode
  • Escape Sequence
  • Octal
  1. In single character values, only one character will be assigned to a variable,that must be enclosed with single quotes.
  2. ASCII value will returns the integer ASCII code. In java every value that enclosed with a single quote will have an integer value. ASCII values range lies in 0 - 255
  3. Escape Sequence is a combination to two characters. First character will be backslash ( \ ), which is used to print specific characters. they are also known as White Space Characters.     i.e. \' , \'', \t, \n, \b, \f, \r, \\ 
  4. In Unicode, value is always started from \u followed by four hexa decimal digits. It is stands for Universal Code.Whenever compiler find Unicode in the program it will converts it to its corresponding value.
  5. In Octal, value is always started from backslash ( \ ).value must be enclosed with single quotes. Octal values range lies in \0 - \377.


In the above diagram: Decimal represents - ASCII values, Hex represents - Unicode value(in \u0020 format), Oct represents - Octal values, Character represents - Character values.


Boolean Literals : They includes only true and false values, that can be assigned only to compatible type. true, false values will always be in lower case because they are predefined values that cannot be changed.

String Literals : It include - str.
It is a collection of 0 and more characters in double quotes.They are represent as char array in java memory.
String also consider Escape sequence, Unicode, Octal values.

Example:
        String s = "Welcome to my blog.";

  • Null and Empty String :
                 No value assigned to string will be represent as Empty String.
                 Null value assigning to string will be represented as Null String.In this object will not be created and reference variable contains null value.

Example:
           String s = "";         //Empty String.
           String s1 = null;    //Null String.

Null Literals : Null is a value ad a default value for any reference variable.
It is also a default value for String variable.

Comments

Post a Comment

Popular posts from this blog

Data Types

 Data Types     There are different types of data used by programmers in java like Numbers, Alphabets, Images etc. Computer does not know the difference between values like number or alphabets.Each type of data will be categorized into different types which are known as Datatypes in java. Thus, Datatype is a classification of data according to its type that an object or a variable can hold. It is a kind of data storage that contains specific type or range of value. Whenever variable is created a storage is assigned to it according to the datatype. Two main concepts of Datatypes are - Type of data. Size allocated to it. It is classified into two main categories i.e Primitives and Referenced .  There are 8 Primitive Type Datatypes and 4 Referenced Datatypes i.e Primitives Datatypes - These are built-in or predefined by the language and their definition cannot be changed. byte short char int long float double boolean Referenced Dat...

Polymorphism

Polymorphism Polymorphism means " Many Forms " - One Object with many behaviors. In other words, ability to appear more than one i.e. to do different things with single object. It can be implemented by method overloading and method overriding in java. For eg. : When you press a device it will turn on, when you press it again it will turn off. Similarly in java "+" Plus Operator has polymorphic behavior: It can perform Arithmetic Operation as well as Concatenation Operation Types of Polymorphism Static Polymorphism Dynamic Polymorphism Static Polymorphism also represented as Compile Time  Polymorphism and Early Binding  Polymorphism. Static Polymorphism is just a method overloading. Thus, it is a process of defining more than one functionalities with same name in same class but having different parameter lists (i.e. different no. of parameters list or different parameters type) Here compiler will figure out which method to call by...