Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java Literals

                         

By literal we mean any number, text, or other information that represents a value. This means what you type is what you get. We will use literals in addition to variables in Java statement. While writing a source code as a character sequence, we can specify any value as a literal such as an integer. This character sequence will specify the syntax based on the value's type. This will give a literal as a result. For instance

int month  = 10;

In the above statement the literal is an integer value i.e 10. The literal is 10 because it directly represents the integer value.

In Java programming language there are some special type of literals that represent numbers, characters, strings and boolean values. Lets have a closer look on each of the following.

Integer Literals
Integer literals is a sequence of digits and a suffix as L. To represent the type as long integer we use L as a suffix. We can specify the integers either in decimal, hexadecimal or octal format. To indicate a decimal format put the left most digit as nonzero. Similarly put the characters as ox to the left of at least one hexadecimal digit to indicate hexadecimal format. Also we can indicate the octal format by a zero digit followed by the digits 0 to 7. Lets tweak the table below.

 659L  Decimal integer literal of type long integer
 0x4a  Hexadecimal integer literal of type integer
 057L  Octal integer literal of type long integer

 
Character Literals
We can specify a character literal as a single printable character in a pair of single quote characters such as 'a', '#', and '3'. You must be knowing about the ASCII character set. The ASCII character set includes 128 characters including letters, numerals, punctuations etc. There are few character literals which are not readily printable through a keyboard. The table below shows the codes that can  represent these special characters. The letter d such as in the octal, hex etc represents a number. 

 Escape  Meaning
 \n  New line
 \t  Tab
 \b  Backspace
 \r  Carriage return
 \f  Formfeed
 \\  Backslash
 \'  Single quotation mark
 \"  Double quotation mark
 \d  Octal
 \xd  Hexadecimal
 \ud  Unicode character

It is very interesting to know that if we want to specify a single quote, a backslash, or a nonprintable character as a character literal use an escape sequence. An escape sequence uses a special syntax to represents a character. The syntax begins with a single backslash character.
Lets see the table below in which the character literals use Unicode escape sequence to represent printable and nonprintable characters both.

 'u0041'  Capital letter A
 '\u0030'  Digit 0
 '\u0022'  Double quote "
 '\u003b'  Punctuation ;
 '\u0020'  Space
 '\u0009'  Horizontal Tab 

Boolean Literals
The values true and false are also treated as literals in Java programming. When we assign a value to a boolean variable, we can only use these two values. Unlike C, we can't presume that the value of 1 is equivalent to true and 0 is equivalent to false in Java. We have to use the values true and false to represent a Boolean value. Like   
boolean chosen = true;
Remember that the literal true is not represented by the quotation marks around it. The Java compiler will take it as a string of characters, if its in quotation marks.

Floating-point literals
Floating-point numbers are like real numbers in mathematics, for example, 4.13179, -0.000001. Java has two kinds of floating-point numbers: float and double. The default type when you write a floating-point literal is double.

Type
Size
Range
Precision
name
bytes
bits
approximate
in decimal digits
float
4
32
+/- 3.4 * 1038
6-7
double
8
64
+/- 1.8 * 10308
15

A floating-point literal can be denoted as a decimal point, a fraction part, an exponent (represented by E or e) and as an integer. We also add a suffix to the floating point literal as D, d, F or f.  The type of a floating-point literal defaults to double-precision floating-point.
The following floating-point literals represent double-precision floating-point and floating-point values.

 6.5E+32 (or 6.5E32)  Double-precision floating-point literal
 7D  Double-precision floating-point literal
 .01f  Floating-point literal

String Literals
The string of characters is represented as String literals in Java. In Java a string is not a basic data type, rather it is an object. These strings are not stored in arrays as in C language. There are few methods provided in Java to combine strings, modify strings and to know whether to strings have the same value.
We represent string literals as
String myString = "How are you?";
The above example shows how to represent a string. It consists of
a series of characters inside double quotation marks.

Lets see some more examples of string literals:

""                                      // the empty string
"\""                                   // a string containing "
"This is a string"               // a string containing 16 characters
"This is a " +                   // actually a string-valued constant expression,
"two-line string"             // formed from two string literals

Strings can include the character escape codes as well, as shown here:
String example = "Your Name, \"Sumit\"";
System.out.println("Thankingyou,\nRichards\n");

Null Literals
The final literal that we can use in Java programming is a Null literal. We specify the Null literal in the source code as 'null'. To reduce the number of references to an object, use null literal. The type of the null literal is always null. We typically assign null literals to object reference variables. For instance

s = null;

An this example an object is referenced by s. We reduce the number of references to an object by assigning null to s. Now, as in this example the object is no longer referenced so it will be available for the garbage collection i.e. the compiler will destroy it and the free memory will be allocated to the other object. Well, we will later learn about garbage collection.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
source code for chat p
to set combox
check whether number
how to check the char
low level design
HTML Images Text Over
struts tiles
addtwonumbersinjavausi
java.awt.
struts 2.0 file upload
count column tabl
form validation using
java create form
Struts 2 Validation Ex
Insert Data into Datab
comments in jsp page
clear the console in j
getmonths
video player in java a
countthenumberofdigits
vector in java
Main-Class
UML basics: The compon
Introducing the UML
criteria restrictions
flush
how to read xml file i
Retrieving Data From d
scjp1.5
ListIterator
create zip file in str
video streaming UDP
complet code for searc
break?Ä??¬Ä??????????
TextField
Combattons la programm
java.lang.ArrayIndexOu
Visual Basic Networkin
how to use hiperlink
delete data in mysql d
Flash Audio Converting
Enabling and Disabling
JTestbutton code
dojo servlets
insert values into ora
Photoshop Effects Parc
sample program toTake
flex api
phone formate
<t:dataScroller
Detached
appletexample
Refresh a Web Page Usi
parallel processing
Dictionary
string to array
Photoshop Basics Photo
objective type questio
accessing database fro
draw triangle
Class Variables and Me
hidden
json lib/componen...]=
json lib/componen...]=
retreiving data from a
json lib/componen...]=
???е??°Ñ???в???????Ð
ip
B Tree
gwt ext tree structure
Javascript DHTML Javas
how create addition (s
Request Object In JSP
java program count and
MultiAction in Spring
how to use an applet i
combobox using ajax
how to create forum
java trees
java.oi.package progra
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.