0% found this document useful (0 votes)
2 views20 pages

Java Tutorial

The document provides an introduction to Java programming, including a simple program example and essential concepts such as compiling and running Java code, identifiers, literals, comments, data types, and operators. It also covers control flow statements like if-else and switch statements, as well as iteration statements such as while, do-while, and for loops. Additionally, it explains variable declaration, dynamic initialization, type conversion, and casting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views20 pages

Java Tutorial

The document provides an introduction to Java programming, including a simple program example and essential concepts such as compiling and running Java code, identifiers, literals, comments, data types, and operators. It also covers control flow statements like if-else and switch statements, as well as iteration statements such as while, do-while, and for loops. Additionally, it explains variable declaration, dynamic initialization, type conversion, and casting.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Programming

 A First Simple Program


/*
This is a simple Java program.
Call this file "[Link]".
*/
class Example
{
public static void main(String args[])
{
[Link]("Welcome to Java");
}
}
 Note –
The name of the class should match the name of the file that holds the program. You should also make sure that the
Capitalization of the filename matches the class name.. the reason for this is that java is case-sensitive..
 Compiling the program –
c:\>javac <file name>.java
• Example – c:\>javac [Link]
 Run the program –
c:\>java <class name>
• Example – c:\>java Example
 Whitespace
Java is a free-form language. This means that you do not need to follow any
special indentation rules. For example program could have been written all on
one line or in any other strange way you felt like typing it, as long as there was
at least one whitespace character between each token that was not already
delineated by an operator or separator. In java, whitespace is space, tab, or
newline.
 Identifiers
Identifiers are used for class names, method names, and variable names. An
identifier may be any descriptive sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters. They must not begin
with a number, least they be confused with a numerical literal. Again, java is
case-sensitive, so VALUE is different identifier than Value. Some examples of
valid identifiers are:
AvgTemp count a4 $test this_is_ok
Invalid variable names include: 2count high-temp Not/ok
 Literals
A constant value in Java is created by using a literal representation of it. For
example, here are some literals:
100 98.6 'X' "This is a test“
 Comments
There are three types of comments defined by Java.
//  single-line comments
/* Statements */  Multiline comments
/** Statements */  Documentation comments
 Separators
In Java, there are a few characters that are used as separators. The most
commonly used separator in java is semicolon. The separators are shown in the
following table:
Symbol Name
() Parentheses
{} Braces
[] Brackets
Symbol Name
; Semicolon
, Comma
. Period
 Java Keywords
There are 48 reserved keywords. These keywords cannot be used as names for a
variable, class, or method.
Example: break const if new long try etc.
 Data Types
Java defines eight simple (or elemental) types of data: byte, short, int, long,
char, float, double, and Boolean. These can be put in four groups:
 Integers This group includes byte, short, int, & long, which are for
whole-valued signed numbers.
 Floating-point numbers This group includes float & double, which
represent numbers with fractional precision.
 Characters This group includes char, which represents symbols in a
character set, like letters and numbers.
 Boolean This group includes Boolean, which is special type for
representing true / false values.
 Integers
Name Width Range
Long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Int 32 -2,147,483,648 to 2,147,483,647
Short 16 -32,768 to 32,767
Byte 8 -128 to 127
 Floating-point Types
Name Width in Bits Range
Double 64 1.7e-308 to 1.7e+308
Float 32 3.4e-038 to 3.4e+038
 Characters
Name Width in Bits Range
Char 16 0 to 65,536
 Booleans
Java has a simple type, called Boolean, for logical values. It can have only one
of two possible values, true or false.
 Character Literals
Escape Sequence Description
\ddd Octal character (ddd)
\uxxxx Hexadecimal UNICODE character (xxxx)
\' Single quote
\" Double quote
\\ Backslash
\r Carriage rerutn
\n New line
\f Form feed
\t Tab
\b Backspace
 Variables –
 Declaring a Variable
type identifier [=value][,identifier [=value] …];
 Example
int d=3, e, f=5; char x=‘f’; double pi = 3.14159;
 Dynamic Initialization
class DynInit
{
public static void main(String args[])
{
double a = 3.0, b = 4.0;
double c = [Link](a*a + b*b);
//c is dynamically initialoized
[Link]("Hypotenuse is " + c);
}
}
 Type Conversion & Casting
The general form: (target-type) value;
Example –
int a;
int b;
b = (byte) a;
OPERATORS
 Arithmetic Operators
Operator Result
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition Assignment
-= Subtraction Assignment
*= Multiplication Assignment
/= Division Assignment
%= Modulus Assignment
-- Decrement
 Bitwise Operators
Operator Result
` Bitwise Unary NOT
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
>> Shift right
>>> Shift right Zero fill
<< Shift left
&= Bitwise AND assignment
!= Bitwise OR assignment
^= Bitwise Exclusive OR assignment
>>= Shift right assignment
>>>= Shift right Zero fill assignment
<<= Shift left assignment
 Relational Operators
Operator Result
== Equal to
!= Not Equal
> Greater Than
< Less Than
>= Greater than or equal to
<= Less than or equal to
 Boolean Logical Operators
Operator Result
& Logical AND
| Logical OR
^ Logical XOR (Exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else
The If Statement
Syntax Program
if(condition) statement; class ifsample
{
Example public static void main(String args[]) {
if(num<100) int x,y;
x = 10;
println(“num is less than 100”); y = 20;
if(x<y) [Link]("X is less than Y");
x = x * 2;
if(x==y) [Link]("X now equal to Y");
x = x * 2;
if(x>y) [Link]("X now greater than Y");
}
}
Output –
X is less than Y
X now equal to Y
X now greater than Y
 If – Else Statement
Syntax:
if (condition)
True statement;
else
False statement;
Example:
int a,b;
a = 10;
b = 20;
if (a<b)
[Link](“a is less than b");
else
[Link](“b is less than a");
 If – else - if Ladder
Syntax:
if (condition)
True statement;
else if (condition)
True statement;
else if (condition)
True statement;
.
.
else
False statement;
 If – else - if Ladder
Program:
class ifelse {
public static void main(String args[]) {
int month = 4; //April
String session;

if(month == 12 || month == 1 || month == 2)


session = "Winter";
else if(month == 3 || month == 4 || month == 5)
session = "Spring";
else if(month == 6 || month == 7 || month == 8)
session = "Summer";
else if(month == 9 || month == 10 || month == 11)
session = "Autumn";
else
session = "Bogus Month";

[Link]("April is in the " + session + ".");


}
}
 Switch Statement
Syntax:
switch (expression) {
case value1:
statement;
break;
case value2:
statement;
break;
.
.
case valueN:
statement;
break;
default:
default statement;
}
 Switch Statement
Program:
class SampleSwitch {
public static void main(String args[]) {
int d=1;
switch(d) {
case 0:
[Link]("SunDay");
break;
case 1:
[Link]("MonDay");
break;
case 2:
[Link]("TuesDay");
break;
case 3:
[Link]("WedDay");
break;
case 4:
[Link]("ThursDay");
break;
case 5:
[Link]("FriDay");
break;
case 6:
[Link]("SaturDay");
break;
default:
[Link]("Wrong Choice");
}

}
}
 Iteration Statement Program:
 While class SampleWhile {
public static void main(String args[]) {
Syntax:
int i=0;
While (condition) { while(i < 10)
//Body of loop {
[Link](i);
}
i++;
Example: }
int i;
}
while (i < 10)
}
{
[Link](i);
i++;
}
 Iteration Statement Program:
 Do - While class SampleDoWhile {
public static void main(String args[]) {
Syntax:
int i=0;
do { do
// Body of loop {
[Link](i);
} while (condition);
i++;
Example: } while (i<10);
int i; }
}
do
{
[Link](i);
i++;
} while (i<10);
 Iteration Statement
 For
Syntax:
for (initialization; condition; iteration) {
// body
Program:
} class SampleFor {
Example: public static void main(String args[]) {
int i; int i;
for(i=0;i<10;i++)
for(i=0;i<10;i++) [Link](i);
{ }
[Link](i); }
}

You might also like