You are on page 1of 36

Java Tutorial Java Data Type, Operator

Introduction
1 Learn how to use Java int type
2 Create your first Java program
3 How to define more complex Java program
4 What are Java Keywords and Identifiers
5 What are variables in a Java program
Primitive Data Types
6 What are the primitive data types in Java
7 Java boolean type
8 Java char type
9 Java char value escape
10 Java byte type
11 Java short type
12 Java long type
13 Java float type
14 Java double type
15 Java String type
16 Java String scape
17 Java String Concatenation
Operator
18 Java !rithmetic "perators
19 !rithmetic Compound !ssignment "perators
20 Java Increment and #ecrement "perator
21 Java Logical "perators
22 Java Logical "perators Shortcut
23 Java $elational "perators
24 Java %itwise "perators
25 Java Left Shift "perator
26 Java $ight Shift "perator
27 Java &nsigned $ight Shift
28 Java ternary operator
29 &se of instance comparisons with instanceof operator in Java
Main
30 &se of argv to get an integer value from command line in Java
Java int type
Description
When byte and short values are used in an expression they are promoted
to int when the expression is evaluated.
i!e and value
int is a si"ned #$%bit type that has a ran"e &rom %$,'(),(*#,+(* to
$,'(),(*#,+().
octal inte"er,base ei"ht-
Octal values are denoted in Java by a leadin" !ero. valid value ./ will
produce an error &rom the compiler, since / is outside o& octal0s . to )
ran"e.
public class Main 1
public static void main,trin"23 ar"s- 1
int i 4 .'.5
ystem.out.println,i-5
6
6
The output7
hexadecimal inte"er,base '+-
hexadecimal matches with modulo * word si!es, such as *, '+, #$, and +(
bits. 8ou si"ni&y a hexadecimal constant with a leadin" !ero%x, ,.x or
.9-.
The ran"e o& a hexadecimal di"it is . to ':, so ; throu"h < ,or a
throu"h & - are substituted &or '. throu"h ':.
;n inte"er literal can always be assi"ned to a lon" variable. ;n inte"er
can also be assi"ned to a char as lon" as it is within ran"e.
public class Main1
public static void main,trin"23 ar"v-1
int & 4 .9<<<<<5
ystem.out.println,&-5=='.(*:):
6
6
The code above "enerates the &ollowin" result.
8our &irst Java pro"ram
>reate Java &ile
?et0s start by compilin" and runnin" the &ollowin" short sample pro"ram.
=@
This is a simple Java pro"ram. >all this &ile AMain.BavaA.
@=
public class Main 1
== 8our pro"ram be"ins with a call to main,-.
public static void main,trin" ar"s23- 1
ystem.out.println,AJava.A-5
6
6
In Java, a source &ile is called a compilation unit. It is a text &ile
that contains one or more class de&initions. The Java compiler reCuires
that a source &ile use the .Bava &ilename extension.
In Java, all code must reside inside a class. Dy convention, the name o&
the public class should match the its &ile name. ;nd Java is case%
sensitive.
The code above "enerates the &ollowin" result.
>ompilin" the Pro"ram
To compile the pro"ram, execute the compiler, Bavac, speci&yin" the name
o& the source &ile on the command line7
>7EFBavac Main.Bava
The Bavac compiler creates a &ile called Main.class. Main.class contains
the byte code version o& the pro"ram.
To run the pro"ram, use the Java interpreter, called Bava. Pass the
class name Main as a command%line ar"ument, as shown here7
>7EFBava Main
When the pro"ram is run, the &ollowin" output is displayed7

When Java source code is compiled, each individual class is put into its
own &ile named classname.class.
; >loser ?ooG at the Main.Bava
The &irst part is a comment.
=@
This is a simple Java pro"ram. >all this &ile AMain.BavaA.
@=
>omment is a remarG &or a pro"ram. The contents o& a comment are i"nored
by the compiler. The next line o& code in the pro"ram is shown here7
public class Main 1
The Geyword class declares that a new class is bein" de&ined. Main is
the name o& the class. The entire class de&inition is between the
openin" curly brace ,1- and the closin" curly brace ,6-. The next line
in the pro"ram is the sin"le%line comment, shown here7
== 8our pro"ram be"ins with a call to main,-.
; sin"le%line comment be"ins with a == and ends at the end o& the line.
The next line o& code is shown here7
public static void main,trin" ar"s23- 1
Java applications be"in execution by callin" main,trin" ar"s23-. Java
is case%sensitive. Thus, Main is di&&erent &rom main.
8our second Java pro"ram
; hort Pro"ram with a variable
; variable is a memory location that may be assi"ned a value. The value
o& a variable is chan"eable.
The &ollowin" code de&ines a variable and chan"e its value by assi"nin"
a new value to it.
public class Main 1
public static void main,trin" ar"s23- 1
int num5 == a variable called num
num 4 '..5
ystem.out.println,AThis is num7 A H num-5
num 4 num @ $5
ystem.out.print,AThe value o& num @ $ is A-5
ystem.out.println,num-5
6
6
When you run this pro"ram, you will see the &ollowin" output7

The &ollowin" snippet declares an inte"er variable called num. Java
reCuires that variables must be declared be&ore they can be used.
int num5 == this declares a variable called num
<ollowin" is the "eneral &orm o& a variable declaration7
type var%name5
In the pro"ram, the line assi"ns to num the value '...
num 4 '..5 == this assi"ns num the value '..
De&ine more than one variable with comma
To declare more than one variable o& the speci&ied type, you may use a
comma%separated list o& variable names.
public class Main 1
public static void main,trin" ar"s23- 1
int num, num$5
num 4 '..5 == assi"ns num the value '..
num$ 4 $..5
ystem.out.println,AThis is num7 A H num-5
ystem.out.println,AThis is num$7 A H num$-5
6
6
When the pro"ram is run, the &ollowin" output is displayed7
Isin" DlocGs o& >ode
Java can "roup two or more statements into blocGs o& code. >ode blocG is
enclosin" the statements between openin" and closin" curly braces,16-.
<or example, a blocG can be a tar"et &or Java0s i& and &or statements.
>onsider this i& statement7
public class Main 1
public static void main,trin" ar"s23- 1
int x, y5
x 4 '.5
y 4 $.5
i& ,x J y- 1 == be"in a blocG
x 4 y5
y 4 .5
ystem.out.println,Ax4A H x-5
ystem.out.println,Ay4A H y-5
6 == end o& blocG
6
6
Kere is the output o& the code above7

; blocG o& code as the tar"et o& a &or loop.
public class Main 1
public static void main,trin" ar"s23- 1
int i, y5
y 4 $.5
&or ,i 4 .5 i J '.5 iHH- 1 == the tar"et o& this loop is a blocG
ystem.out.println,AThis is i7 A H i-5
ystem.out.println,AThis is y7 A H y-5
y 4 y % '5
6
6
6
Java Leywords and Identi&iers
<ull list o& Geywords in Java
; Geyword is a word whose meanin" is de&ined by the pro"rammin"
lan"ua"e. Java Geywords and reserved Words7
abstract class extends implements null strict&p true
assert const &alse import pacGa"e super try
boolean continue &inal instanceo& private switch void
breaG de&ault &inally int protected synchroni!ed volatile
byte do &loat inter&ace public this while
case double &or lon" return throw
catch else "oto native short throws
char enum i& new static transient
;n identi&ier is a word used by a pro"rammer to name a variable, method,
class, or label. Leywords and reserved words may not be used as
identi&iers. ;n identi&ier must be"in with a letter, a dollar si"n ,M-,
or an underscore ,N-5 subseCuent characters may be letters, dollar
si"ns, underscores, or di"its.
ome examples are7
&oobar == le"al
Myclass == le"al
Ma == le"al
#Na == ille"al7 starts with a di"it
OthePalue == ille"al7 bad 'st char
Java Identi&iers are case sensitive. <or example, myPalue and MyPalue
are distinct identi&iers.
Isin" identi&iers
Identi&iers are used &or class names, method names, and variable names.
;n identi&ier may be any seCuence o& uppercase and lowercase letters,
numbers, or the underscore and dollar%si"n characters. Identi&iers must
not be"in with a number. Java Identi&iers are case%sensitive. The
&ollowin" code illustrates some examples o& valid identi&iers7
public class Main 1
public static void main,trin"23 ar"v- 1
int ;TQT, count, i', M;test, thisNisNaNtest5
6
6
The &ollowin" code shows invalid variable names include7
public class Main 1
public static void main,trin"23 ar"v-1
int $count, h%l, a=b,
6
6
I& you try to compile this code, you will "et the &ollowin" error
messa"e7
Java Pariable
Declarin" a Pariable
; variable is de&ined by an identi&ier, a type, and an optional
initiali!er. The variables also have a scope,visibility = li&etime-.
In Java, all variables must be declared be&ore they can be used. The
basic &orm o& a variable declaration is shown here7
type identi&ier 2 4 value32, identi&ier 24 value3 ...3 5
There are three parts in variable de&inition7
31 typecould be int or &loat.
32 identi&ieris the variable0s name.
33 Initiali!ation includes an eCual si"n and a value.
To declare more than one variable o& the speci&ied type, use a comma%
separated list.
int a, b, c5 == declares three ints, a, b, and c.
int d 4 #, e, & 4 :5 == declares three more ints, initiali!in" d and &.
The &ollowin" variables are de&ined and initiali!ed in one expression.
public class Main 1
public static void main,trin"23 ar"v- 1
byte ! 4 $5 == initiali!es !.
double pi 4 #.'(5 == declares an approximation o& pi.
char x 4 0x05 == the variable x has the value 0x0.
6
6
Pariable cannot be used prior to its declaration.
public class Main 1
public static void main,trin"23 ar"v- 1
count 4 '..5 == >annot use count be&ore it is declaredO
int count5
6
6
>ompilin" the code above "enerates the &ollowin" error messa"e7
;ssi"nment Operator
The assi"nment operator is the sin"le eCual si"n, 4. It has this "eneral
&orm7
var 4 expression5
type o& var must be compatible with the type o& expression. The
assi"nment operator allows you to create a chain o& assi"nments.
public class Main 1
public static void main,trin"23 ar"v- 1
int x, y, !5
x 4 y 4 ! 4 '..5 == set x, y, and ! to '..
ystem.out.println,Ax is A H x-5
ystem.out.println,Ay is A H y-5
ystem.out.println,A! is A H !-5
6
6
The output7
Dynamic Initiali!ation
Java allows variables to be initiali!ed dynamically. In the &ollowin"
code the Math.sCrt returns the sCuare root o& $ @ $ and assi"ns the
result to c directly.
public class Main 1
public static void main,trin" ar"s23- 1
== c is dynamically initiali!ed
double c 4 Math.sCrt,$ @ $-5
ystem.out.println,Ac is A H c-5
6
6
The output &rom the code above is
Primitive Data Types
Java Primitive Data Types
Java ei"ht primitive types
Java de&ines ei"ht primitive types o& data7 byte, short, int, lon",
char, &loat, double, and boolean.
Primitive Type
Reserved
Word
i!e
Min
Palue
Max Palue
Doolean boolean S=; S=; S=;
>haracter char '+%bit Inicode .
Inicode $
'+
%
'
Dyte inte"er byte *%bit %'$* H'$)
hort inte"er short '+%bit %$
':
H$
':
% '
Inte"er int #$%bit %$
#'
H$
#'
% '
?on" inte"er lon" +(%bit %$
+#
H$
+#
% '
<loatin"%point &loat #$%bit '.(e%.(: #.(eH.#*
Double precision &loatin"%
point
double +(%bit (./e%#$( '.*eH#.*
byte, short, int, and lon" are &or whole%valued si"ned numbers. &loat
and double are &ractional precision numbers.
char represents symbols in a character set, liGe letters and numbers.
boolean represents true=&alse values.
Java Inte"ers
Java de&ines &our inte"er types7 byte, short, int, and lon".
Inte"er types are si"ned and can have positive and ne"ative values.
The width and ran"es o& these inte"er types vary widely7
Same Width Ran"e
lon" +( %/,$$#,#)$,.#+,*:(,)):,*.* to /,$$#,#)$,.#+,*:(,)):,*.)
int #$ %$,'(),(*#,+(* to $,'(),(*#,+()
short '+ %#$,)+* to #$,)+)
byte * %'$* to '$)
<loatin" Point Types
There are two Ginds o& &loatin"%point types7 &loat and double. &loat
type represents sin"le%precision numbers. double type stores double%
precision numbers.
<loatin"%Point Types width and ran"es are shown here7
Same
Width in
Dits
;pproximate Ran"e
double +( (./e%#$( to '.*eH#.*
&loat #$ '.(e%.(: to #.(eH.#*
Java boolean type
Description type
Java has a boolean type &or lo"ical values. This is the type returned by
all relational operators.
Palue
It can have only one o& two possible values, true or &alse.
?iterals
Doolean literals are only two lo"ical values7 true and &alse. The values
o& true and &alse do not convert into any numerical representation.
The true literal in Java does not eCual ', nor does the &alse literal
eCual .. In Java, they can only be assi"ned to variables declared as
boolean.
Doolean class
The Doolean class wraps a primitive type boolean in an obBect. ;n obBect
o& type Doolean contains a sin"le &ield whose type is boolean.
Doolean class has the methods &or convertin" a boolean to a trin" and a
trin" to a boolean.
Qxample
Kere is a pro"ram that demonstrates the boolean type7
public class Main 1
public static void main,trin" ar"s23- 1
boolean boolPariable5
boolPariable 4 &alse5
ystem.out.println,Ab is A H boolPariable-5
boolPariable 4 true5
ystem.out.println,Ab is A H boolPariable-5
6
6
Output7
Qxample $
The true literal in Java does not eCual ', nor does the &alse literal
eCual .. In Java, they can only be assi"ned to variables declared as
boolean.
public class Main 1
public static void main,trin"23 ar"v- 1
boolean b 4 true5
int i 4 b5
6
6
I& you try to compile the pro"ram, the &ollowin" error messa"e will be
"enerated by compiler.
Java char type
Description
In Java, char stores characters. Java uses Inicode to represent
characters. Inicode can represent all o& the characters &ound in all
human lan"ua"es.
i!e
Java char is a '+%bit type.
Palue Ran"e
The ran"e o& a char is . to +:,:#+. There are no ne"ative chars.
?iterals
>haracters in Java are indices into the Inicode character set. character
is represented inside a pair o& sin"le Cuotes. <or example, 0a0, 0!0,
and 0T0.
Qxample
Kere is a pro"ram that demonstrates char variables7
public class Main 1
public static void main,trin" ar"s23- 1
char ch', ch$5
ch' 4 **5 == code &or 9
ch$ 4 0805
ystem.out.print,Ach' and ch$7 A-5
ystem.out.println,ch' H A A H ch$-5==ch' and ch$7 9 8
6
6
The code above "enerates the &ollowin" result.

ch' is assi"ned the value **, which is the ;>II ,and Inicode- value
that corresponds to the letter 9.
char type value can be used as an inte"er type and you can per&orm
arithmetic operations.
public class Main 1
public static void main,trin" ar"s23- 1
char ch'5
ch' 4 0905
ystem.out.println,Ach' contains A H ch'-5==ch' contains 9
ch' 4 ,char-,ch' H '-5 == increment ch'
ystem.out.println,Ach' is now A H ch'-5==ch' is now 8
6
6
Qxample $
public class Main 1
public static void main,trin"23 ar"v- 1
char ch 4 0a05
ystem.out.println,Ach is A H ch-5==ch is a
ch 4 0T05
ystem.out.println,Ach is A H ch-5==ch is T
ch 4 0U05
ystem.out.println,Ach is A H ch-5==ch is U
ch 4 0M05
ystem.out.println,Ach is A H ch-5==ch is M
ch 4 0V05
ystem.out.println,Ach is A H ch-5==ch is V
6
6
The code above "enerates the &ollowin" result.
Qxample #
The &ollowin" code stores unicode value into a char variable. The
unicode literal uses Euxxxx &ormat.
public class Main 1
public static void main,trin"23 ar"s- 1
int x 4 ):5
char y 4 ,char- x5
char hal& 4 0Eu..;D05
ystem.out.println,Ay is A H y H A and hal& is A H hal&-5
6
6
The code above "enerates the &ollowin" result.
Java char value escape
Description
The escape seCuences are used to enter impossible%to%enter%directly
characters.
yntax
0E00 is &or the sin"le%Cuote character. 0En0 is &or the newline
character.
Qxample
<or octal notation, use the bacGslash &ollowed by the three%di"it
number. <or example, 0E'('0 is the letter 0a0.
<or hexadecimal, you enter a bacGslash%u ,Eu-, then exactly &our
hexadecimal di"its. <or example, 0Eu..+'0 is the IO%?atin%' 0a0 because
the top byte is !ero. 0Eua(#$0 is a Japanese LataGana character.
public class Main 1
public static void main,trin"23 ar"v- 1
char ch 4 0E005
ystem.out.println,Ach is A H ch-5==ch is 0
6
6
>haracter is a simple wrapper around a char.
The code above "enerates the &ollowin" result.
Qscape value list
The &ollowin" table shows the character escape seCuences.
Qscape
eCuence
Description
Eddd Octal character ,ddd-
Euxxxx Kexadecimal Inicode character ,xxxx-
E0 in"le Cuote
EA Double Cuote
EE DacGslash
Er >arria"e return
En Sew line
E& <orm &eed
Et Tab
Eb DacGspace
Java byte type
Description
The smallest inte"er type is byte. byte type variables are use&ul when
worGin" with a stream o& data &rom a networG or &ile.
Dyte variables are declared by use o& the byte Geyword. The &ollowin"
declares two byte variables called b and c7
byte b, c5
i!e and value
byte is a si"ned *%bit type that has a ran"e &rom %'$* to '$).
Qxample
The &ollowin" code creates two byte type variables and assi"ns values.
public class Main 1
public static void main,trin"23 ar"s- 1
byte b' 4 '..5
byte b$ 4 $.5
ystem.out.println,APalue o& byte variable b' is 7A H b'-5
ystem.out.println,APalue o& byte variable b' is 7A H b$-5
66
The code above "enerates the &ollowin" result.

The Dyte class wraps a value o& primitive type byte in an obBect. Dyte
class provides several methods &or convertin" a byte to a trin" and a
trin" to a byte.
Java short type
Description
The si!e o& Java short type is between byte and inte"er.
i!e and value
short is a si"ned '+%bit type. short type variable has a ran"e &rom
%#$,)+* to #$,)+).
Qxample
Kere are some examples o& short variable declarations7
short s5
short t5
Java lon" type
Description
Java lon" type is used when an int type is not lar"e enou"h.
i!e and value
lon" is a si"ned +(%bit type and . The ran"e o& lon" type is
%/,$$#,#)$,.#+,*:(,)):,*.* to /,$$#,#)$,.#+,*:(,)):,*.)
?iterals
To speci&y a lon" literal, you need to tell the compiler that the
literal value is o& type lon" by appendin" an upper% or lowercase ? to
the literal. <or example, .x)&&&&&&&&&&&&&&? or '$#'$#'$#'$#?.
Qxample
The &ollowin" code creates a lon" type literal and assi"ns the value to
a lon" type variable.
public class Main 1
public static void main,trin" ar"s23- 1
lon" l 4 .x)&&&&&&&&&&&&&&?5
ystem.out.println,Al is A H l-5
6
6
The output "enerated by this pro"ram is shown here7
Qxample $
Kere is a pro"ram that use lon" type to store the result.
public class Main 1
public static void main,trin" ar"s23- 1
lon" result4 ,lon"-Inte"er.M;9NP;?IQ @ ,lon"-'.5
ystem.out.println,result-5==$'()(*#+().
6
6
The result could not have been held in an int variable.
The code above "enerates the &ollowin" result.
Java &loat type
&loat type
&loat type represents sin"le%precision numbers.
&loat type variables are use&ul when you need a &ractional component.
Kere are some example &loat variable declarations7
&loat hi"h, low5
Palue and si!e
&loat is #$%bit width and its ran"e is &rom '.(e%.(: to #.(eH.#*
approximately.
?iterals
<loatin"%point literals in Java de&ault to double precision. To speci&y
a &loat literal, you must append an < or & to the constant.
Qxample '
The &ollowin" code shows how to declare &loat literals.
public class Main 1
public static void main,trin" ar"s23- 1
&loat d 4 #.'(':/<5
ystem.out.print,d-5==#.'(':/
6
6
The code above "enerates the &ollowin" result.
Java double type
Description
Java double type represents double%precision numbers.
i!e and value
double is +(%bit width and its ran"e is &rom (./e%#$( to '.*eH#.*
approximately.
Qxample
Kere is a pro"ram that uses double variables to compute the area o& a
circle7
public class Main 1
public static void main,trin" ar"s23- 1
double pi, r, a5
r 4 '..****5 == radius o& circle
pi 4 #.'(':/$+5 == pi, approximately
a 4 pi @ r @ r5
ystem.out.println,A;rea o& circle is A H a-5
6
6
The output7
?iterals
double type numbers have decimal values with a &ractional component.
They can be expressed in either standard or scienti&ic notation.
tandard notation consists o& a whole number component &ollowed by a
decimal point &ollowed by a &ractional component. <or example, $..,
#.'(':/, and ..+++).
public class Main 1
public static void main,trin" ar"s23- 1
double d 4 #.'(':/5
ystem.out.print,d-5==#.'(':/
6
6
The code above "enerates the &ollowin" result.
?iteral ?etter
8ou can explicitly speci&y a double literal by appendin" a D or d.
public class Main 1
public static void main,trin" ar"s23- 1
double d 4 #.'(':/D5
ystem.out.print,d-5==#.'(':/
6
6
The code above "enerates the &ollowin" result.
cienti&ic notation
cienti&ic notation uses a standard%notation, &loatin"%point number plus
a su&&ix that speci&ies a power o& '. by which the number is to be
multiplied. The exponent is indicated by an Q or e &ollowed by a decimal
number, which can be positive or ne"ative. <or example, +..$Q$#,
#'(':/Q%.:, and (eH'...
public class Main 1
public static void main,trin"23 ar"v- 1
double d' 4 +..$$Q$#5
double d$ 4 #'(':/Q%.:5
double d# 4 $eH'..5
ystem.out.println,Ad' is A H d'-5
ystem.out.println,Ad$ is A H d$-5
ystem.out.println,Ad# is A H d#-5
6
6
The output "enerated by this pro"ram is shown here7
double value constant
Java0s &loatin"%point calculations are capable o& returnin" Hin&inity,
%in&inity, H..., %..., and SaS
dividin" a positive number by ... returns Hin&inity. <or example,
ystem.out.println,'..=...-5 outputs In&inity.
public class Main1
public static void main,trin"23 ar"s- 1
ystem.out.println,'..=...-5
6
6
The code above "enerates the &ollowin" result.
double In&inity
Dividin" a ne"ative number by ... outputs %in&inity. <or example,
ystem.out.println,%'..=...-5 outputs %In&inity.
public class Main1
public static void main,trin"23 ar"s- 1
ystem.out.println,%'..=...-5 6
6
Output7
double SaS
Dividin" ... by ... returns SaS. sCuare root o& a ne"ative number is
SaS. <or example, ystem.out.println,...=...- and
ystem.out.println,Math.sCrt,%'..-- output SaS.
Dividin" a positive number by Hin&inity outputs H.... <or example,
ystem.out.println,'..=,'..=...--5 outputs H....
Dividin" a ne"ative number by Hin&inity outputs %.... <or example,
ystem.out.println,%'..=,'..=...--5 outputs %....
public class Main 1
public static void main,trin"23 ar"s- 1
Double d' 4 new Double,H...-5
ystem.out.println,d'.doublePalue,--5
Double d$ 4 new Double,%...-5
ystem.out.println,d$.doublePalue,--5
ystem.out.println,d'.eCuals,d$--5
ystem.out.println,H... 44 %...-5
6
6
The code above "enerates the &ollowin" result.
Java trin" type
Description
The trin" class represents character strin"s. ; Cuoted strin" constant
can be assi"ned to a trin" variable.
?iteral
trin" literals in Java are speci&ied by enclosin" a seCuence o&
characters between a pair o& double Cuotes. In Java strin"s are actually
obBect types.
Qxample
Declare trin" type variable.
public class Main1
public static void main,trin"23 ar"v-1
trin" str 4 Athis is a test &rom Bava.comA5
ystem.out.println,str-5
6
6
eCuals,- vs 44
eCuals, - method and the 44 operator per&orm two di&&erent operations.
eCuals, - method compares the characters inside a trin" obBect. The 44
operator compares two obBect re&erences to see whether they re&er to the
same instance.
The &ollowin" pro"ram shows the di&&erences7
public class Main 1
public static void main,trin" ar"s23- 1
trin" s' 4 Ademo.comA5
trin" s$ 4 new trin",s'-5
ystem.out.println,s' H A eCuals A H s$ H A %F A H s'.eCuals,s$--5
ystem.out.println,s' H A 44 A H s$ H A %F A H ,s' 44 s$--5
6
6
Kere is the output o& the precedin" example7
Java trin" Qscape
Description
The escape seCuences are used to enter impossible%to%enter%directly
strin"s.
yntax
<or example, AEAA is &or the double%Cuote character. AEnA &or the
newline strin".
<or octal notation, use the bacGslash &ollowed by the three%di"it
number. <or example, AE'('A is the letter AaA.
<or hexadecimal, you enter a bacGslash%u ,Eu-, then exactly &our
hexadecimal di"its. <or example, AEu..+'A is the IO%?atin%' AaA because
the top byte is !ero. AEua(#$A is a Japanese LataGana character.
Qscape ?ist
The &ollowin" table summari!es the Java trin" escape seCuence.
Qscape
eCuence
Description
Eddd Octal character ,ddd-
Euxxxx Kexadecimal Inicode character ,xxxx-
E0 in"le Cuote
EA Double Cuote
EE DacGslash
Er >arria"e return
En Sew line
E& <orm &eed
Et Tab
Eb DacGspace
Qxample
Qxamples o& strin" literals with escape are
AKello WorldA
AtwoEnlinesA
AEAThis is in CuotesEAA
The &ollowin" example escapes the new line strin" and double Cuotation
strin".
public class Main 1
public static void main,trin"23 ar"v- 1
trin" s 4 ABava.comA5
ystem.out.println,As is A H s-5
s 4 AtwoEnlinesA5
ystem.out.println,As is A H s-5
s 4 AEACuotesEAA5
ystem.out.println,As is A H s-5
6
6
Qxample $
Java trin" literials must be be"in and end on the same line. I& your
strin" is across several lines, the Java compiler will complain about
it.
public class Main 1
public static void main,trin"23 ar"v-1
trin" s 4 Aline '
line $
A5
6
6
Java trin" >oncatenation
Description
8ou can use H operator to concatenate strin"s to"ether.
Qxample '
<or example, the &ollowin" &ra"ment concatenates three strin"s7
public class Main 1
public static void main,trin"23 ar"v- 1
trin" a"e 4 A/A5
trin" s 4 AKe is A H a"e H A years old.A5
ystem.out.println,s-5 6
6
Qxample $
The &ollowin" code uses strin" concatenation to create a very lon"
strin".
public class Main 1
public static void main,trin" ar"s23- 1
trin" lon"tr 4 A; Bava comA H
AD B a v a .c o m A H
A> Bava comA H
AD Bava.com .A5
ystem.out.println,lon"tr-5
6
6
Qxample #
8ou can concatenate strin"s with other types o& data.
public class Main 1
public static void main,trin"23 ar"v- 1
int a"e 4 '5
trin" s 4 AKe is A H a"e H A years old.A5
ystem.out.println,s-5
6
6
The output7
Qxample (
De care&ul when you mix other types o& operations with strin"
concatenation. >onsider the &ollowin"7
public class Main 1
public static void main,trin"23 ar"v- 1
trin" s 4 A&our7 A H $ H $5
ystem.out.println,s-5 6
6
This &ra"ment displays

rather than the

To complete the inte"er addition &irst, you must use parentheses, liGe
this7
trin" s 4 A&our7 A H ,$ H $-5
Sow s contains the strin" A&our7 (A.
Operator

You might also like