http://www.hudatutorials.

com
What is a Language? What is a Program?
A Program is a set of operations grouped in a single file.

JAVA

A Language is a communication media between two persons or two things.

What is Java?
Java is a programming language and computing platform first released by Sun Microsystems in 1995.

Is Java free to download?
Yes, Java is free to download. Get the latest version at http://java.com.

JAVA Basics
Java is a simple, portable, distributed, robust, secure, dynamic, architecture neutral, object oriented programming language. And its syntax is like C/C++ Language syntax. It is divided into three categories. 1. Java Standard Edition (JSE) 2. Java Enterprise Edition (JEE) 3. Java Mobile Edition (JME)

Java Standard Edition (JSE)
It is used to create Standalone Applications as well as Graphical User Interface (GUI) Applications.

Java Enterprise Edition (JEE)
It is used to create Web Applications as well as Enterprise Applications.

Java Mobile Edition (JME)
It is used to create Mobile Applications.

JAVA Data Types
Data Type boolean byte char short int float long double Range true or false -128 to 127 any character or 0 to 65535 -32768 to 32767 -2147483648 to 2147483647 1.40129846432481707e-45 to 3.40282346638528860e+38 -9223372036854775808 to 9223372036854775807 4.94065645841246544e-324d to 1.79769313486231570e+308d Memory
(in bytes)

1 bit 1 2 2 4 4 8 8

Default Value false 0 '\u0000' 0 0 0.0f 0L 0.0d

JAVA Keywords
These are reserved words. So you cannot use as identifiers. The keywords const and goto are reserved, even though they are not currently used. Java compiler produce error messages if these are appear in programs.

1

http://www.hudatutorials.com
abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while

Operators in JAVA
Type Arithmetic Assignment Arithmetic Assignment Logical Relational Increment and Decrement Bitwise Separator Conditional (Ternary) Operator +, -, *, /, % = +=, -=, *=, /= !, ||, && ==, !=, >, <, >=, <= ++, -&, |, ^, <<, >>, >>> , ?:

JAVA Control Flow Statements

It is sometimes necessary to perform repeated actions or skip some statements in a program. For these actions certain control statements are available. These statements control the flow of execution of the programs.

Decision Making Statements
if if – else switch

Looping or Iteration Statements
for while do – while

Branching Statements
break continue return

Commenting
// Single Line Comment /* */ Multi Line Comment /** */ Documentation Comment

Software Requirements for Creating Java Programs
Any one of the following Notepad or any Text Editor NetBeans IDE

2

http://www.hudatutorials.com
Eclips IDE

Software Requirements to Run Java
Java Development Kit (JDK) for Compilation Java Runtime Environment (JRE) for Running Java Programs

Your First JAVA Program
public class MyFirstJavaProgram { public static void main(String args[]) { System.out.println("WELCOME to HudaTutorials.com"); } } Before going to execute the above program, we have to know the rules of JAVA program. Rule 1: JAVA Program name should be the class name. MyFirstJavaProgram in the case of above program with .java extension. So you should type the above program in Notepad of windows OS. And save the file with MyFirstJavaProgram.java in the location C:\HudaTutorials.com\ Before running the above program you should know how to run dos prompt and some basic commands. Go to start menu  Programs  Accessories  click on Command Prompt You will get the DOS command prompt window. Type MD C:\HudaTutorials.com at the command prompt then press enter. After type CD C:\HudaTutorials.com press again enter. Your prompt changes to C:\HudaTutorials.com> here we knowing the MD command which is creates a directory and another command is CD it is changing the current directory. Type java –version at your command prompt and then press enter, it shows which JAVA version is installed on your computer. The recommended JAVA Version is 1.6.0.14 it is called as JAVA 6 Version. Then type javac at your command prompt and then press enter. It shows some text that means your environmental variables are working fine. If you want to run your first JAVA program, you should follow following steps. 1. Compile the JAVA program. 2. Run the JAVA program.

Compilation
Type javac MyFirstJavaProgram.java then press enter. It does not show any message that means your JAVA program compiled successfully. If any text showing except your command prompt there are some errors in your program. Check the program then correct the errors then compile again. After successful compilation the javac compiler creates MyFirstJavaProgram.class file in c:\HudaTutorials.com. To check if class file is created type DIR then press enter at your dos prompt.

Running

3

http://www.hudatutorials.com
Type java MyFirstJavaProgram then press enter. It shows the message WELCOME to HudaTutorials.com. You are successfully compiled and ran the program.

Explanation for MyFirstJavaProgram.java
Like C programs JAVA program execution starts from public static void main(String args[]) method. The System.out.println("WELCOME to HudaTutorials.com"); print the message WELCOME to HudaTutorials.com on the screen.

JAVA Data Types boolean Data Type
/* boolean Data Type Example */ /* Save with file name BooleanDataType.java */ public class BooleanDataType { public static void main(String args[]) { boolean b; //TYPE DECLARATION DEFAULT VALUE OF boolean IS false System.out.println(); System.out.println("boolean Example"); System.out.println("==============="); System.out.println(); b = true; //ASSIGN A VALUE System.out.println("Assigned boolean Value : " + b); } }

byte Data Type
/* byte Data Type Example */ /* Save with file name ByteDataType.java */ public class ByteDataType { public static void main(String args[]) { byte b; //TYPE DECLARATION DEFAULT VALUE OF byte IS 0 System.out.println(); System.out.println("byte Example"); System.out.println("============"); System.out.println(); b = 77; //ASSIGN A VALUE System.out.println("Assigned byte Value : " + b); } }

char Data Type
/* char Data Type Example */ /* Save with file name CharDataType.java */ public class CharDataType {

4

http://www.hudatutorials.com
public static void main(String args[]) { char c; //TYPE DECLARATION DEFAULT VALUE OF char IS '\u0000' System.out.println(); System.out.println("char Example"); System.out.println("============"); System.out.println(); c = 'A'; //ASSIGN A VALUE System.out.println("Assigned char Value : " + c); c = 97; //ASSIGN small a ascii value System.out.println("Assigned char ASCII Value : " + c); } }

short Data Type
/* short Data Type Example */ /* Save with file name ShortDataType.java */ public class ShortDataType { public static void main(String args[]) { short s; //TYPE DECLARATION DEFAULT VALUE OF short IS 0 System.out.println(); System.out.println("short Example"); System.out.println("============="); System.out.println(); s = 123; //ASSIGN A VALUE System.out.println("Assigned short Value : " + s); } }

int Data Type
/* int Data Type Example */ /* Save with file name IntDataType.java */ public class IntDataType { public static void main(String args[]) { int i; //TYPE DECLARATION DEFAULT VALUE OF int IS 0 System.out.println(); System.out.println("int Example"); System.out.println("============="); System.out.println(); i = 5; //ASSIGN A VALUE System.out.println("Assigned int Value : " + i); } }

float Data Type
/* float Data Type Example */ /* Save with file name FloatDataType.java */

5

http://www.hudatutorials.com
public class FloatDataType { public static void main(String args[]) { float a; //TYPE DECLARATION DEFAULT VALUE OF float IS 0f System.out.println(); System.out.println("float Example"); System.out.println("============="); System.out.println(); /* ASSIGN A VALUE LIKE THIS COMPILER SHOW ERROR */ /* POSSIBLE LOSS OF PRECISION FOUND DOUBLE REQUIRED FLOAT */ /*a = 5.25; //GET ERROR COMPILER ASSUMES double */ a = 5.25f; //ASSIGN A float VALUE System.out.println("Assigned float Value : " + a);

} }

long Data Type
/* long Data Type Example */ /* Save with file name LongDataType.java */ public class LongDataType { public static void main(String args[]) { long a; //TYPE DECLARATION DEFAULT VALUE OF long IS 0L System.out.println(); System.out.println("long Example"); System.out.println("============="); System.out.println(); a = 45000L; //ASSIGN A VALUE System.out.println("Assigned long Value : " + a); } }

double Data Type
/* double Data Type Example */ /* Save with file name DoubleDataType.java */ public class DoubleDataType { public static void main(String args[]) { double a; //TYPE DECLARATION DEFAULT VALUE OF double IS 0d System.out.println(); System.out.println("double Example"); System.out.println("=============="); System.out.println(); a = 5.25d; //ASSIGN A double VALUE System.out.println("Assigned double Value : " + a); } }

6

http://www.hudatutorials.com
JAVA Keywords Definitions abstract
A Java keyword used in a class definition to specify that a class is not to be instantiated, but rather inherited by other classes. An abstract class can have abstract methods that are not implemented in the abstract class, but in subclasses.

abstract class

A class that contains one or more abstract methods, and therefore can never be instantiated. Abstract classes are defined so that other classes can extend them and make them concrete by implementing the abstract methods.

abstract method
A method that has no implementation.

assert
An assertion is a statement in the JAVA programming language that enables you to test your assumptions about your program.

boolean
Refers to an expression or variable that can have only a true or false value. The Java programming language provides the boolean type and the literal values true and false.

break
A Java keyword used to resume program execution at the statement immediately following the current statement. If followed by a label, the program resumes execution at the labeled statement.

byte
A sequence of eight bits. Java provides a corresponding byte type.

case
A Java keyword that defines a group of statements to begin executing if a value specified matches the value defined by a preceding switch keyword.

catch

A Java keyword used to declare a block of statements to be executed in the event that a Java exception, or run time error, occurs in a preceding try block.

char
A Java keyword used to declare a variable of type character.

class
In the Java programming language, a type that defines the implementation of a particular kind of object. A class definition defines instance and class variables and methods, as well as specifying the interfaces the class implements and the immediate superclass of the class. If the superclass is not explicitly specified, the superclass will implicitly be Object.

class method
A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method.

class variable 7

http://www.hudatutorials.com
A data item associated with a particular class as a whole--not with particular instances of the class. Class variables are defined in class definitions. Also called a static field.

Classpath
An environmental variable which tells the Java Virtual Machine and Java technologybased applications where to find the class libraries, including user-defined class libraries.

const

A reserved Java keyword not used by current versions of the Java programming language.

continue
A Java keyword used to resume program execution at the end of the current loop. If followed by a label, continue resumes execution where the label occurs.

default
A Java keyword optionally used after all case conditions in a switch statement. If all case conditions are not matched by the value of the switch variable, the default keyword will be executed.

do
A Java keyword used to declare a loop that will iterate a block of statements. The loop's exit condition can be specified with the while keyword.

double
A Java keyword used to define a variable of type double.

double precision
In the Java programming language specification, describes a floating point number that holds 64 bits of data.

else

A Java keyword used to execute a block of statements in the case that the test condition with the if keyword evaluates to false.

enum
A Java keyword used to declare an enumerated type.

extends
extends is a JAVA keyword used to inherit the properties and methods of another class. class X extends class Y to add functionality, either by adding fields or methods to class Y, or by overriding methods of class Y. An interface extends another interface by adding methods. Class X is said to be a subclass of class Y.

final
If you declare class, method or field those are not modifiable at any point after declaration. A final class cannot be subclassed, a final method cannot be overridden and a final variable cannot change from its initialized value.

finally

A Java keyword that executes a block of statements regardless of whether a Java Exception, or run time error, occurred in a block defined previously by the try keyword.

float 8

http://www.hudatutorials.com
A Java keyword used to define a floating point number variable.

for
A Java keyword used to declare a loop that reiterates statements. The programmer can specify the statements to be executed, exit conditions, and initialization variables for the loop.

goto if

It is not used by current versions of the Java programming language.

A Java keyword used to conduct a conditional test and execute a block of statements if the test evaluates to true.

implements
A Java keyword included in the class declaration to specify any interfaces that are implemented by the current class.

import
A Java keyword used at the beginning of a source file that can specify classes or entire packages to be referred to later without including their package names in the reference.

instanceof

A two-argument Java keyword that tests whether the runtime type of its first argument is assignment compatible with its second argument.

int
A Java keyword used to define a variable of type integer.

interface
A Java keyword used to define a collection of method definitions and constant values. It can later be implemented by classes that define this interface with the "implements" keyword.

long
A Java keyword used to define a variable of type long.

native
A Java keyword that is used in method declarations to specify that the method is not implemented in the same Java source file, but rather in another language.

new
A Java keyword used to create an instance of a class.

package private

A group of types. Packages are declared with the package keyword.

A Java keyword used in a method or variable declaration. It signifies that the method or variable can only be accessed by other elements of its class.

protected

9

http://www.hudatutorials.com
A Java keyword used in a method or variable declaration. It signifies that the method or variable can only be accessed by elements residing in its class, subclasses, or classes in the same package.

public

A Java keyword used in a class, method or variable declaration. It signifies that the method or variable can be accessed by elements residing in other classes.

return
A Java keyword used to finish the execution of a method. It can be followed by a value required by the method definition.

short
A Java keyword used to define a variable of type short.

static
A Java keyword used to define a variable as a class variable. Classes maintain one copy of class variables regardless of how many instances exist of that class. static can also be used to define a method as a class method. Class methods are invoked by the class instead of a specific instance, and can only operate on class variables.

strictfp
strictfp is a Java keyword used to restrict floating-point calculations to ensure portability. The modifier was introduced into the Java programming language with the Java virtual machine version 1.2. Basically, what it all boils down to is whether or not you care that the results of floatingpoint expressions in your code are fast or predictable. For example, if you need the answers that your code comes up with which uses floating-point values to be consistent across multiple platforms then use strictfp. What Does "strictfp" Do?, When this modifier is specified, the JVM sticks to the Java specifications and returns the consistent value independent of the platform. That is, if you want the answers from your code (which uses floating point values) to be consistent in all platforms, then you need to specify the strictfp modifier. Java's default class Math uses this strictfp modifier as shown below public final strictfp class Math{ }

super
A Java keyword used to access members of a class inherited by the class in which it appears.

switch

A Java keyword used to evaluate a variable that can later be matched with a value specified by the case keyword in order to execute a group of statements.

synchronized
If synchronized keyword applied to a method or code block, guarantees that at most one thread at a time executes that code.

this
It is used to represent an instance of the class in which it appears. this can be used to access class variables and methods.

throw 10

http://www.hudatutorials.com
It is used to throw an exception or any class that implements the "throwable" interface.

throws
It is used in method declarations that specify which exceptions are not handled within the method but rather passed to the next higher level of the program.

transient
It indicates that a field is not part of the serialized form of an object. When an object is serialized, the values of its transient fields are not included in the serial representation, while the values of its non-transient fields are included.

try
A Java keyword that defines a block of statements that may throw a Java language exception. If an exception is thrown, an optional catch block can handle specific exceptions thrown within the try block. Also, an optional finally block will be executed regardless of whether an exception is thrown or not.

void
It is used in method declarations to specify that the method does not return any value. void can also be used as a nonfunctional statement.

volatile
It is used in variable declarations that specifies that the variable is modified asynchronously by concurrently running threads.

while

A Java keyword used to declare a loop that iterates a block of statements. The loop's exit condition is specified as part of the while statement.

JAVA Operators
These are used to manipulate primitive data types. Java operators can be classified as Unary, Binary and Ternary i.e. taking one, two or three arguments. A Unary operator may appear before its argument or after its argument. A Binary or Ternary operator appears between its arguments.

Arithmetic Operators
1) 2) 3) 4) 5) + for Addition (Add two numbers) or Concatenation (Add two strings) - for Subtraction * for Multiplication / for Division % for modulo Division (Remainder)

/* Arithmetic Operators Example */ /* Save with file name ArithmeticOperators.java */ public class ArithmeticOperators { public static void main(String args[]) { System.out.println(); System.out.println("Arithmetic Operators Example"); System.out.println("============================"); System.out.println(); System.out.println("Assignment (20+10) = " + (20+10)); //Assignments System.out.println("Subtraction (20-10) = " + (20-10)); //Subtraction

11

http://www.hudatutorials.com
System.out.println("Multiplication (20X10) = " + (20*10)); //Multiplication System.out.println("Division (20/10) = " + (20/10)); //Disivion System.out.println("Modulo Division (20%12) = " + (20%12)); //Modulo Division } }

Assignment Operator
/* Assignment Operator Example */ /* Save with file name AssignmentOperator.java */ public class AssignmentOperator { public static void main(String args[]) { int a; //VARIABLE DECLARATION System.out.println(); System.out.println("Assignment Operator Example"); System.out.println("==========================="); System.out.println(); a = 10; //ASSIGNMENT System.out.println("Value of a After Assignment : " + a); //PRINTS TEN } }

Arithmetic Assignment Operators
1) 2) 3) 4) += -= *= /=

/* Arithmetic Assignment Operators Example */ /* Save with file name ArithmeticAssignmentOperators.java */ public class ArithmeticAssignmentOperators { public static void main(String args[]) { int a = 1, b = 7; //VARIABLE DECLARATION AND ASSIGN A VALUE a +=10; System.out.println(); System.out.println("Arithmetic Assignment Operators Example"); System.out.println("====================================== ="); System.out.println(); System.out.println("Addition : " + a); b -= 2; System.out.println("Subtraction : " + b); float c; //YOU CAN ALSO DECLARE A VARIABLE AT ANY PLACE c = 3; c *= 5; System.out.println("Multiplication : " + c);

12

http://www.hudatutorials.com
double d = 20; d /= 10; System.out.println("Division : " + d); } }

Logical Operators
Logical operators return true or false value only i.e. the result is always a boolean data type. 1) && - AND 2) || - OR 3) ! - NOT /* Logical Operators Example */ /* Save with file name LogicalOperators.java */ public class LogicalOperators { public static void main(String args[]) { boolean T = true, F = false; //VARIABLES DECLARATION SAME TYPE WITH COMMA OPERATOR System.out.println(); System.out.println("Logical Operators Example"); System.out.println("========================="); System.out.println(); System.out.println("T AND F : " + (T && F)); //TRUE AND FALSE = FALSE System.out.println("T OR F : " + (T || F)); //TRUE OR FALSE = TRUE System.out.println("NOT T : " + (!T)); //NOT TRUE = FALSE } }

Relational Operators
These 1) 2) 3) 4) 5) 6) are used to compare two values or two objects. == EQUAL TO != NOT EQUAL TO > GREATER THAN < LESS THAN >= GREATER THAN OR EQUAL TO <= LESS THAN OR EQUAL TO

/* Relational Operators Example */ /* Save with file name RelationalOperators.java */ public class RelationalOperators { public static void main(String args[]) { System.out.println(); System.out.println("Relational Operators Example"); System.out.println("============================"); System.out.println(); System.out.println("1 Equal to 1 : " + (1==1)); System.out.println("1 Not Equal to 1 : " + (1!=1)); System.out.println("3 Greater Than 2 : " + (3>2)); System.out.println("1 Less Than 2 : " + (1<2));

13

http://www.hudatutorials.com
} } System.out.println("3 Greater Than or Equal to 2 : " + (3>=2)); System.out.println("1 Less Than or Equal to 2 : " + (1<=2));

Increment and Decrement Operators
1) ++ 2) -Increment One Decrement One

These are two types a) Post Increment or Post Decrement b) Pre Increment or pre Decrement /* Increment and Decrement Operators Example */ /* Save with file name IncrementDecrementOperators.java */ public class IncrementDecrementOperators { public static void main(String args[]) { int a = 10, b = 20, c = 30, d = 40; System.out.println(); System.out.println("Increment and Decrement Operators Example "); System.out.println("====================================== ==="); System.out.println(); System.out.println("Post Increment : " + (a++)); //VALUE IS 10 AFTER THIS STATEMENT a VALUE IS 11 System.out.println("After Post Increment : " + a); System.out.println("Post Decrement : " + (b--)); //VALUE IS 20 AFTER THIS STATEMENT b VALUE IS 19 System.out.println("After Post Decrement : " + b); System.out.println("Pre Increment : " + (++c)); //VALUE IS 31 System.out.println("Pre Decrement : " + (--d)); //VALUE IS 39 } }

Bitwise Operators
These are used to manipulate the contents of variables at the bit level. These variables must be of numeric data type (char, short, int, or long). 1) & 2) | 3) ^ 4) << 5) >> 6) >>> /* Bitwise Operators Example */ /* Save with file name BitwiseOperators.java */ public class BitwiseOperators { public static void main(String args[]) { int a = 10, b = 20; System.out.println();

14

http://www.hudatutorials.com
System.out.println("Bitwise Operators Example "); System.out.println("========================="); System.out.println(); System.out.println("a & b : " + (a & b)); System.out.println("a | b : " + (a | b)); System.out.println("a ^ b : " + (a ^ b)); System.out.println("~a : " + (~a)); System.out.println("a << b : " + (a << b)); System.out.println("a >> b : " + (a >> b)); System.out.println("a >>> b : " + (a >>> b)); //There is no unsigned left shift operator } }

Conditional (Ternary) Operator
1) ? 2) : Question Mark colon

The Conditional operator is ternary i.e. it takes three arguments. The operator evaluates the first argument and, if true then evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left /* Conditional Operator Example */ /* Save with file name ConditionalOperator.java */ public class ConditionalOperator { public static void main(String args[]) { int a = 10, b = 20, c, d; c=d=0; //ASSIGN A VALUE TO c, d VARIABLES AT A TIME System.out.println(); System.out.println("Conditional (Ternary) Operator Example "); System.out.println("====================================== "); System.out.println(); c = (a > b ? 100 : b); //c VALUE IS 20 d = (a == 10 ? 0 : 15); //d VALUE IS 0 System.out.println("Value of c : " + c); System.out.println("Value of d : " + d); }

}

15

http://www.hudatutorials.com
JAVA Control Flow Statements
It is sometimes necessary to perform repeated actions or skip some statements in a program. For these actions certain control statements are available. These statements control the flow of execution of the programs.

Decision Making Statements
if if – else switch

if Statement Syntax : if(<condition>) { Statement…1; Statement…2; Statement…n; } /* if Statement Example */ /* Save with file name IfStatement.java */ public class IfStatement { public static void main(String args[]) { int a = 30, b = 40; System.out.println(); System.out.println("if Statement Example "); System.out.println("===================="); System.out.println(); if(a > b) System.out.println("a is greater than b"); System.out.println("Greater Value"); //OUT PUT STATEMENT if(a < b) System.out.println("a is less than b"); //OUT PUT STATEMENT System.out.println("Lesser Value"); //OUT PUT STATEMENT System.out.println(); System.out.println("------- ANOTHER WAY -------"); System.out.println(); /* ANOTHER WAY OF IF STATEMENT */ /* IF THE CONDITION HAS MORE THAN ONE STATEMENT */ /* THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES */ if(a > b) { System.out.println("a is greater than b"); System.out.println("Greater Value"); } if(a < b) { System.out.println("a is less than b"); //OUT PUT STATEMENT

16

http://www.hudatutorials.com
System.out.println("Lesser Value"); //OUT PUT STATEMENT } } } if else Statement Syntax : if(<condition>) { Statement…1; Statement…2; Statement…n; } else { Statement…1; Statement…2; Statement…n; } /* if else Statement Example */ /* Save with file name IfElseStatement.java */ public class IfElseStatement { public static void main(String args[]) { int a = 30, b = 40; System.out.println(); System.out.println("if Else Statement Example "); System.out.println("===================="); System.out.println(); if(a > b) System.out.println("a is greater than b"); else System.out.println("a is less than b"); //OUT PUT STATEMENT System.out.println(); System.out.println("------- ANOTHER WAY -------"); System.out.println(); /* ANOTHER WAY OF IF STATEMENT */ /* IF THE CONDITION HAS MORE THAN ONE STATEMENT */ /* THEN YOU SHOULD USE THOSE STATEMENTS WITH IN BRACES */ if(a > b) { System.out.println("a is greater than b"); System.out.println("Greater Value"); } else { System.out.println("a is less than b"); //OUT PUT STATEMENT System.out.println("Lesser Value"); //OUT PUT STATEMENT }

17

http://www.hudatutorials.com
} } switch Statement Syntax: switch(<value>) { Case…1; Case…2; Case…n; Default…Case; } /* switch Statement Example */ /* Save with file name IntSwitchStatement.java */ public class IntSwitchStatement { public static void main(String args[]) { int a = 2, b = 40; System.out.println(); System.out.println("int Switch Statement Example "); System.out.println("============================"); System.out.println(); /* int value parameter single statement in case */ switch(a) { case 1: System.out.println("a value is One"); case 2: System.out.println("a value is Two"); default: System.out.println("a value is Default Value"); } System.out.println("------------------------"); /* int value parameter Multiple statements in case */ switch(a) { case 1: { System.out.println("Case 1 executed"); System.out.println("a value is One"); break; } case 2: { System.out.println("Case 2 executed"); System.out.println("a value is Two"); break; } default: { System.out.println("Default Case executed");

18

http://www.hudatutorials.com
} } } } /* switch Statement Example */ /* Save with file name CharSwitchStatement.java */ public class CharSwitchStatement { public static void main(String args[]) { char c = 'b'; //char DATA TYPE DECLARATION AND ASSIGN A VALUE System.out.println(); System.out.println("char Switch Statement Example "); System.out.println("============================="); System.out.println(); /* char value parameter single statement in case */ switch(c) { case 'a': System.out.println("case a"); case 'b': System.out.println("case b"); case 'c': System.out.println("case c"); default: System.out.println("default case"); } System.out.println("------------"); /* char value parameter Multiple statements in case */ switch(c) { case 'a': { System.out.println("Case a"); break; } case 'b': { System.out.println("Case b"); break; } case 'c': { System.out.println("Case c"); System.out.println("a value is Two"); break; } default: { System.out.println("a value is Default Value"); break; //NOT REQUIRE BECAUSE LAST STATEMENT

19

http://www.hudatutorials.com
} } } } System.out.println("Default Case"); break; //NOT REQUIRE BECAUSE LAST STATEMENT

Looping or Iteration Statements
for while do – while for loop Syntax : for(<initialization>;<condition>;<increment>) { Statement…1; Statement…2; Statement…n; } Tips If you ignore the braces after loop statement the first statement after loop is executed in loop. If you put ; (semi colon) to loop without braces no statement is executed. If you put ; (semi colon) to loop with braces the body of the loop is executed once. /* for loop Example */ /* Save with file name ForLoop.java */ public class ForLoop { public static void main(String args[]) { System.out.println(); System.out.println("for loop Example "); System.out.println("================"); System.out.println(); for(int count=1 ; count<=10 ; count++) { /* POST INCREMENT */ System.out.println("Loop Statement : " + count); } System.out.println(); System.out.println("Loop 2 output"); System.out.println(); /* ANOTHER WAY */ int i = 0; for(;i<10;) { /* PRE INCREMENT WITH IN THE BODY OF THE LOOP */ System.out.println("Loop Statement : " + ++i); }

20

http://www.hudatutorials.com
System.out.println(); System.out.println("Loop 3 output"); System.out.println(); /* LOOP WITH NO BODY */ int j=0; for(;j<10;j++); // ; (SEMICOLON) IGNORE THE BODY OF THE LOOP { System.out.println("loop body ignore"); } System.out.println("j value after loop execution : " + j); } } while loop Syntax : while(<condition>) { Statement…1; Statement…2; Statement…n; } /* while loop Example */ /* Save with file name WhileLoop.java */ public class WhileLoop { public static void main(String args[]) { System.out.println(); System.out.println("while loop Example "); System.out.println("=================="); System.out.println(); int count = 1; while(count<=10) { /* POST INCREMENT */ System.out.println("Loop Statement : " + count); count++; } } }

do while loop The statements within the do while loop are executed at least once. So the do while loop is called bottom tested loop. Syntax : do { Statement…1; Statement…2; Statement…n; } while(<condition>);

21

http://www.hudatutorials.com
/* do while loop Example */ /* Save with file name DoWhileLoop.java */ public class DoWhileLoop { public static void main(String args[]) { System.out.println(); System.out.println("do while loop Example "); System.out.println("====================="); System.out.println(); int count = 1; do { /* POST INCREMENT */ System.out.println("Loop Statement : " + count); count++; } while(count<=10); } }

Branching Statements
break continue return

break Statement
/* break Statement Example */ /* Save with file name BreakStatement.java */ public class BreakStatement { public static void main(String args[]) { System.out.println(); System.out.println("break Statement Example "); System.out.println("======================="); System.out.println(); int count = 10; while(true) //INFINIT LOOP { if(count == 0) break; System.out.println(count); count--; } } }

continue Statement
/* continue Statement Example */ /* Save with file name ContinueStatement.java */

22

http://www.hudatutorials.com
public class ContinueStatement { public static void main(String args[]) { System.out.println(); System.out.println("continue Statement Example "); System.out.println("=========================="); System.out.println(); int count = 1; while(count<=100) { if(count <= 10) { System.out.println(count); count++; continue; } else { break; } } } }

return Statement
/* return Statement Example */ /* Save with file name ReturnStatement.java */ public class ReturnStatement { public static void main(String args[]) { System.out.println(); System.out.println("return Statement Example "); System.out.println("========================"); System.out.println(); int max = getMaximumNumber(10, 20); if(max==0) System.out.println("Equal Numbers"); else System.out.println("Max Number is : " + max); } //METHOD public static int getMaximumNumber(int x, int y) { if(x > y) return x; else if(x < y) return y; else return 0; } }

23

http://www.hudatutorials.com
JAVA Arrays
An array is a collection of variables of the same type. It stores a fixed size sequential collection of elements of the same type. An array is used to store a collection of data. Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type. If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4. In this tutorial you can learn how to declare arrays, how to assign values to arrays and how get values from arrays. Syntax: <data type> <variable>[]; <data type>[] <variable>; <data type> <variable>[length of array];

boolean Array
It is used to store boolean data type values only. With the following examples you can learn how to declare boolean array, how to assign values to boolean array and how to get values from boolean array. /* boolean Array Example */ /* Save with file name BooleanArray.java */ public class BooleanArray { public static void main(String args[]) { //BOOLEAN ARRAY DECLARATION boolean b[]; //MEMORY ALLOCATION FOR BOOLEAN ARRAY b = new boolean[4]; //ASSIGNING ELEMENTS TO BOOLEAN ARRAY b[0] = true; b[1] = false; b[2] = false; b[3] = false; //BOOLEAN ARRAY OUTPUT System.out.println(); System.out.println("boolean Array Example"); System.out.println("====================="); System.out.println(); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } } } In this example you can learn how to assign values to boolean array at the time of declaration.

24

http://www.hudatutorials.com
/* boolean Array Example 2 */ /* Save with file name BooleanArray2.java */ public class BooleanArray2 { public static void main(String args[]) { //BOOLEAN ARRAY DECLARATION AND ASSIGNMENT boolean b[] = {false,false,true,true}; //BOOLEAN ARRAY OUTPUT System.out.println(); System.out.println("boolean Array Example"); System.out.println("====================="); System.out.println(); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } } } In this example you can learn how to declare boolean array with other boolean variables. /* boolean Array Example 3 */ /* Save with file name BooleanArray3.java */ public class BooleanArray3 { public static void main(String args[]) { //BOOLEAN ARRAY DECLARATION WITH OTHER NORMAL VARIABLES boolean b[], a; //b IS AN ARRAY a IS NOT AN ARRAY //ASSIGN VALUES TO BOOLEAN ARRAY b = new boolean[5]; b[0] = true; b[1] = false; b[2] = false; b[3] = false; a = true; //BOOLEAN ARRAY OUTPUT System.out.println(); System.out.println("boolean Array Example"); System.out.println("====================="); System.out.println(); System.out.println("a value is : "+a); System.out.println(); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } } } In this example you can learn how to assign boolean array to other boolean array. /* boolean Array Example 4 */ /* Save with file name BooleanArray4.java */

25

http://www.hudatutorials.com
public class BooleanArray4 { public static void main(String args[]) { //BOOLEAN ARRAYS DECLARATION boolean[] a, b; //a AND b ARE ARRAY VARIABLES //ASSIGN VALUES TO BOOLEAN ARRAY b = new boolean[5]; b[0] = true; b[1] = false; b[2] = false; b[3] = false; //ASSIGNING b ARRAY TO a ARRAY VARIABLE a = b; //BOOLEAN ARRAY OUTPUT System.out.println(); System.out.println("boolean Array Example"); System.out.println("====================="); System.out.println(); System.out.println("b array values"); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } System.out.println(); System.out.println("a array values"); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }

byte Array
It is used to store byte data type values only. With the following examples you can learn how to declare byte array, how to assign values to byte array and how to get values from byte array. /* byte Array Example */ /* Save with file name ByteArray.java */ public class ByteArray { public static void main(String args[]) { //BYTE ARRAY DECLARATION byte b[]; //MEMORY ALLOCATION FOR BYTE ARRAY b = new byte[4]; //ASSIGNING ELEMENTS TO BYTE ARRAY b[0] = 20; b[1] = 10; b[2] = 30; b[3] = 5;

26

http://www.hudatutorials.com
//BYTE ARRAY OUTPUT System.out.println(); System.out.println("byte Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } } }

In this example you can learn how to assign values to byte array at the time of declaration. /* byte Array Example 2 */ /* Save with file name ByteArray2.java */ public class ByteArray2 { public static void main(String args[]) { //BYTE ARRAY DECLARATION AND ASSIGNMENT byte b[] = {20,10,30,5}; //BYTE ARRAY OUTPUT System.out.println(); System.out.println("byte Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } } } In this example you can learn how to declare byte array with other byte variables. /* byte Array Example 3 */ /* Save with file name ByteArray3.java */ public class ByteArray3 { public static void main(String args[]) { //BYTE ARRAY DECLARATION byte b[], a; //b IS AN ARRAY a IS NOT AN ARRAY //MEMORY ALLOCATION FOR BYTE ARRAY b = new byte[4]; //ASSIGNING ELEMENTS TO BYTE ARRAY b[0] = 20; b[1] = 10; b[2] = 30; b[3] = 5; a = 100; //BYTE ARRAY OUTPUT System.out.println();

27

http://www.hudatutorials.com
System.out.println("byte Array Example"); System.out.println("=================="); System.out.println(); System.out.println("a value is : "+a); System.out.println(); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } } }

In this example you can learn how to assign byte array to other byte array. /* byte Array Example 4 */ /* Save with file name ByteArray4.java */ public class ByteArray4 { public static void main(String args[]) { //BYTE ARRAY DECLARATION byte[] a, b; //a AND b ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR BYTE ARRAY b = new byte[4]; //ASSIGNING ELEMENTS TO BYTE ARRAY b[0] = 20; b[1] = 10; b[2] = 30; b[3] = 5; //ASSIGNING b ARRAY TO a ARRAY VARIABLE a = b; //BYTE ARRAY OUTPUT System.out.println(); System.out.println("byte Array Example"); System.out.println("=================="); System.out.println(); System.out.println("b array values"); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + b[i]); } System.out.println(); System.out.println("a array values"); for(int i=0;i<b.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }

char Array
It is used to store char data type values only. With the following examples you can learn how to declare char array, how to assign values to char array and how to get values from char array.

28

http://www.hudatutorials.com
/* char Array Example */ /* Save with file name CharArray.java */ public class CharArray { public static void main(String args[]) { //CHAR ARRAY DECLARATION char c[]; //MEMORY ALLOCATION FOR CHAR ARRAY c = new char[4]; //ASSIGNING ELEMENTS TO CHAR ARRAY c[0] = 'a'; c[1] = 'c'; c[2] = 'D'; c[3] = 'B'; //CHAR ARRAY OUTPUT System.out.println(); System.out.println("char Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<c.length;i++) { System.out.println("Element at Index : "+ i + " " + c[i]); } } } In this example you can learn how to assign values to char array at the time of declaration. /* char Array Example 2 */ /* Save with file name CharArray2.java */ public class CharArray2 { public static void main(String args[]) { //CHAR ARRAY DECLARATION AND ASSIGNMENT char c[] = {'a', 'c', 'D', 'B'}; //CHAR ARRAY OUTPUT System.out.println(); System.out.println("char Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<c.length;i++) { System.out.println("Element at Index : "+ i + " " + c[i]); } } } In this example you can learn how to declare char array with other char variables. /* char Array Example 3 */ /* Save with file name CharArray3.java */

29

http://www.hudatutorials.com
public class CharArray3 { public static void main(String args[]) { //CHAR ARRAY DECLARATION char c[], a;//c IS AN ARRAY a IS NOT AN ARRAY //MEMORY ALLOCATION FOR CHAR ARRAY c = new char[4]; //ASSIGNING ELEMENTS TO CHAR ARRAY c[0] = 'a'; c[1] = 'c'; c[2] = 'D'; c[3] = 'B'; a = 'X'; //CHAR ARRAY OUTPUT System.out.println(); System.out.println("char Array Example"); System.out.println("=================="); System.out.println(); System.out.println("a value is : "+a); System.out.println(); for(int i=0;i<c.length;i++) { System.out.println("Element at Index : "+ i + " " + c[i]); } } } In this example you can learn how to assign char array to other char array. /* char Array Example 4 */ /* Save with file name CharArray4.java */ public class CharArray4 { public static void main(String args[]) { //CHAR ARRAY DECLARATION char[] c, a;//c AND a ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR CHAR ARRAY c = new char[4]; //ASSIGNING ELEMENTS TO CHAR ARRAY c[0] = 'a'; c[1] = 'c'; c[2] = 'D'; c[3] = 'B'; //ASSIGNING c ARRAY TO a ARRAY VARIABLE a = c; //CHAR ARRAY OUTPUT System.out.println(); System.out.println("char Array Example"); System.out.println("=================="); System.out.println(); System.out.println("c array values"); for(int i=0;i<c.length;i++) { System.out.println("Element at Index : "+ i + " " + c[i]);

30

http://www.hudatutorials.com
} System.out.println(); System.out.println("a array values"); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); }

} }

short Array
It is used to store short data type values only. With the following examples you can learn how to declare short array, how to assign values to short array and how to get values from short array. /* short Array Example */ /* Save with file name ShortArray.java */ public class ShortArray { public static void main(String args[]) { //SHORT ARRAY DECLARATION short s[]; //MEMORY ALLOCATION FOR SHORT ARRAY s = new short[4]; //ASSIGNING ELEMENTS TO SHORT ARRAY s[0] = 10; s[1] = 30; s[2] = 40; s[3] = 7; //SHORT ARRAY OUTPUT System.out.println(); System.out.println("short Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } } } In this example you can learn how to assign values to short array at the time of declaration. /* short Array Example 2 */ /* Save with file name ShortArray2.java */ public class ShortArray2 { public static void main(String args[]) { //SHORT ARRAY DECLARATION AND ASSIGNMENT short s[] = {10,30,40,7}; //SHORT ARRAY OUTPUT

31

http://www.hudatutorials.com
System.out.println(); System.out.println("short Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } } } In this example you can learn how to declare short array with other short variables. /* short Array Example 3 */ /* Save with file name ShortArray3.java */ public class ShortArray3 { public static void main(String args[]) { //SHORT ARRAY DECLARATION short s[], s2; //s IS AN ARRAY s2 IS NOT AN ARRAY //MEMORY ALLOCATION FOR SHORT ARRAY s = new short[4]; //ASSIGNING ELEMENTS TO SHORT ARRAY s[0] = 10; s[1] = 30; s[2] = 40; s[3] = 7; s2 = 101; //SHORT ARRAY OUTPUT System.out.println(); System.out.println("short Array Example"); System.out.println("=================="); System.out.println(); System.out.println("value of s2 : "+s2); System.out.println(); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } } } In this example you can learn how to assign short array to other short array. /* short Array Example 4 */ /* Save with file name ShortArray4.java */ public class ShortArray4 { public static void main(String args[]) { //SHORT ARRAY DECLARATION short[] s, s2; //s AND s2 ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR SHORT ARRAY s = new short[4];

32

http://www.hudatutorials.com
//ASSIGNING ELEMENTS TO SHORT ARRAY s[0] = 10; s[1] = 30; s[2] = 40; s[3] = 7; //ASSIGNING s ARRAY TO s2 ARRAY VARIABLE s2 = s; //SHORT ARRAY OUTPUT System.out.println(); System.out.println("short Array Example"); System.out.println("=================="); System.out.println(); System.out.println("s array values"); for(int i=0;i<s.length;i++) { System.out.println("Element at Index : "+ i + " " + s[i]); } System.out.println(); System.out.println("s2 array values"); for(int i=0;i<s2.length;i++) { System.out.println("Element at Index : "+ i + " " + s2[i]); } } }

int Array
It is used to store int data type values only. With the following examples you can learn how to declare int array, how to assign values to int array and how to get values from int array. /* int Array Example */ /* Save with file name IntArray.java */ public class IntArray { public static void main(String args[]) { //INT ARRAY DECLARATION int n[]; //MEMORY ALLOCATION FOR INT ARRAY n = new int[4]; //ASSIGNING ELEMENTS TO INT ARRAY n[0] = 5; n[1] = 3; n[2] = 4; n[3] = 7; //INT ARRAY OUTPUT System.out.println(); System.out.println("int Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<n.length;i++) { System.out.println("Element at Index : "+ i + " " + n[i]);

33

http://www.hudatutorials.com
} } }

In this example you can learn how to assign values to int array at the time of declaration. /* int Array Example 2 */ /* Save with file name IntArray2.java */ public class IntArray2 { public static void main(String args[]) { //INT ARRAY DECLARATION int n[] = {5,3,4,7}; //INT ARRAY OUTPUT System.out.println(); System.out.println("int Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<n.length;i++) { System.out.println("Element at Index : "+ i + " " + n[i]); } } } In this example you can learn how to declare int array with other int variables. /* int Array Example 3 */ /* Save with file name IntArray3.java */ public class IntArray3 { public static void main(String args[]) { //INT ARRAY DECLARATION int n[], n2; //n IS AN ARRAY n2 IS NOT AN ARRAY //MEMORY ALLOCATION FOR INT ARRAY n = new int[4]; //ASSIGNING ELEMENTS TO INT ARRAY n[0] = 5; n[1] = 3; n[2] = 4; n[3] = 7; n2 = 555; //INT ARRAY OUTPUT System.out.println(); System.out.println("int Array Example"); System.out.println("=================="); System.out.println(); System.out.println("n2 value is : "+n2); System.out.println(); for(int i=0;i<n.length;i++) { System.out.println("Element at Index : "+ i + " " + n[i]);

34

http://www.hudatutorials.com
} } }

In this example you can learn how to assign int array to other int array. /* int Array Example 4 */ /* Save with file name IntArray4.java */ public class IntArray4 { public static void main(String args[]) { //INT ARRAY DECLARATION int[] n, n2; //n AND n2 ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR INT ARRAY n = new int[4]; //ASSIGNING ELEMENTS TO INT ARRAY n[0] = 5; n[1] = 3; n[2] = 4; n[3] = 7; //ASSIGNING n ARRAY TO n2 ARRAY VARIABLE n2 = n; //INT ARRAY OUTPUT System.out.println(); System.out.println("int Array Example"); System.out.println("=================="); System.out.println(); System.out.println("n array values"); System.out.println(); for(int i=0;i<n.length;i++) { System.out.println("Element at Index : "+ i + " " + n[i]); } System.out.println(); System.out.println("n2 array values"); for(int i=0;i<n2.length;i++) { System.out.println("Element at Index : "+ i + " " + n2[i]); } } }

float Array
It is used to store float data type values only. With the following examples you can learn how to declare float array, how to assign values to float array and how to get values from float array. /* float Array Example */ /* Save with file name FloatArray.java */ public class FloatArray { public static void main(String args[])

35

http://www.hudatutorials.com
{ //FLOAT ARRAY DECLARATION float f[]; //MEMORY ALLOCATION FOR FLOAT ARRAY f = new float[4]; //ASSIGNING ELEMENTS TO FLOAT ARRAY f[0] = 10.10f; f[1] = 30.3f; f[2] = 40.60f; f[3] = 77.50f; //FLOAT ARRAY OUTPUT System.out.println(); System.out.println("float Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<f.length;i++) { System.out.println("Element at Index : "+ i + " " + f[i]); } } } In this example you can learn how to assign values to float array at the time of declaration. /* float Array Example 2 */ /* Save with file name FloatArray2.java */ public class FloatArray2 { public static void main(String args[]) { //FLOAT ARRAY DECLARATION float f[] = {10.10f,30.3f,40.60f,77.50f}; //FLOAT ARRAY OUTPUT System.out.println(); System.out.println("float Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<f.length;i++) { System.out.println("Element at Index : "+ i + " " + f[i]); } } } In this example you can learn how to declare float array with other float variables. /* float Array Example 3 */ /* Save with file name FloatArray3.java */ public class FloatArray3 { public static void main(String args[]) { //FLOAT ARRAY DECLARATION float f[], f2; //f IS AN ARRAY f2 IS NOT AN ARRAY

36

http://www.hudatutorials.com
//MEMORY ALLOCATION FOR FLOAT ARRAY f = new float[4]; //ASSIGNING ELEMENTS TO FLOAT ARRAY f[0] = 10.10f; f[1] = 30.3f; f[2] = 40.60f; f[3] = 77.50f; f2 = 555.55f; //FLOAT ARRAY OUTPUT System.out.println(); System.out.println("float Array Example"); System.out.println("=================="); System.out.println(); System.out.println("f2 value is : "+f2); System.out.println(); for(int i=0;i<f.length;i++) { System.out.println("Element at Index : "+ i + " " + f[i]); } } } In this example you can learn how to assign float array to other float array. /* float Array Example 4 */ /* Save with file name FloatArray4.java */ public class FloatArray4 { public static void main(String args[]) { //FLOAT ARRAY DECLARATION float[] f, f2; //f AND f2 ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR FLOAT ARRAY f = new float[4]; //ASSIGNING ELEMENTS TO FLOAT ARRAY f[0] = 10.10f; f[1] = 30.3f; f[2] = 40.60f; f[3] = 77.50f; //ASSIGNING f ARRAY TO f2 ARRAY VARIABLE f2 = f; //FLOAT ARRAY OUTPUT System.out.println(); System.out.println("float Array Example"); System.out.println("=================="); System.out.println(); System.out.println("f array values"); System.out.println(); for(int i=0;i<f.length;i++) { System.out.println("Element at Index : "+ i + " " + f[i]); } System.out.println(); System.out.println("f2 array values"); for(int i=0;i<f2.length;i++) {

37

http://www.hudatutorials.com
System.out.println("Element at Index : "+ i + " " + f2[i]); } } }

long Array
It is used to store long data type values only. With the following examples you can learn how to declare long array, how to assign values to long array and how to get values from long array. /* long Array Example */ /* Save with file name LongArray.java */ public class LongArray { public static void main(String args[]) { //LONG ARRAY DECLARATION long a[]; //MEMORY ALLOCATION FOR LONG ARRAY a = new long[4]; //ASSIGNING ELEMENTS TO LONG ARRAY a[0] = 100000L; a[1] = 300000L; a[2] = 400000L; a[3] = 786777L; //LONG ARRAY OUTPUT System.out.println(); System.out.println("long Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } } In this example you can learn how to assign values to long array at the time of declaration. /* long Array Example 2 */ /* Save with file name LongArray2.java */ public class LongArray2 { public static void main(String args[]) { //LONG ARRAY DECLARATION long a[] = {100000L,300000L,400000L,786777L}; //LONG ARRAY OUTPUT System.out.println(); System.out.println("long Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<a.length;i++)

38

http://www.hudatutorials.com
{ } } } In this example you can learn how to declare long array with other long variables. /* long Array Example 3 */ /* Save with file name LongArray3.java */ public class LongArray3 { public static void main(String args[]) { //LONG ARRAY DECLARATION long a[], a2; //a IS AN ARRAY a2 IS NOT AN ARRAY //MEMORY ALLOCATION FOR LONG ARRAY a = new long[4]; //ASSIGNING ELEMENTS TO LONG ARRAY a[0] = 100000L; a[1] = 300000L; a[2] = 400000L; a[3] = 786777L; a2 = 222222L; //LONG ARRAY OUTPUT System.out.println(); System.out.println("long Array Example"); System.out.println("=================="); System.out.println(); System.out.println("a2 value is : "+a2); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } } In this example you can learn how to assign long array to other long array. /* long Array Example 4 */ /* Save with file name LongArray4.java */ public class LongArray4 { public static void main(String args[]) { //LONG ARRAY DECLARATION long[] a, a2; //a AND a2 ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR LONG ARRAY a = new long[4]; //ASSIGNING ELEMENTS TO LONG ARRAY a[0] = 100000L; a[1] = 300000L; a[2] = 400000L; a[3] = 786777L; System.out.println("Element at Index : "+ i + " " + a[i]);

39

http://www.hudatutorials.com
//ASSIGNING a ARRAY TO a2 ARRAY VARIABLE a2 = a; //LONG ARRAY OUTPUT System.out.println(); System.out.println("long Array Example"); System.out.println("=================="); System.out.println(); System.out.println("a array values"); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } System.out.println(); System.out.println("a2 array values"); System.out.println(); for(int i=0;i<a2.length;i++) { System.out.println("Element at Index : "+ i + " " + a2[i]); } } }

double Array
It is used to store double data type values only. With the following examples you can learn how to declare double array, how to assign values to double array and how to get values from double array. /* double Array Example */ /* Save with file name DoubleArray.java */ public class DoubleArray { public static void main(String args[]) { //DOUBLE ARRAY DECLARATION double a[]; //MEMORY ALLOCATION FOR DOUBLE ARRAY a = new double[4]; //ASSIGNING ELEMENTS TO DOUBLE ARRAY a[0] = 100000d; a[1] = 300000d; a[2] = 400000d; a[3] = 786777d; //DOUBLE ARRAY OUTPUT System.out.println(); System.out.println("double Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }

40

http://www.hudatutorials.com
In this example you can learn how to assign values to double array at the time of declaration. /* double Array Example 2 */ /* Save with file name DoubleArray2.java */ public class DoubleArray2 { public static void main(String args[]) { //DOUBLE ARRAY DECLARATION double a[] = {100000d,300000d,400000d,786777d}; //DOUBLE ARRAY OUTPUT System.out.println(); System.out.println("double Array Example"); System.out.println("=================="); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } } In this example you can learn how to declare double array with other double variables. /* double Array Example 3 */ /* Save with file name DoubleArray3.java */ public class DoubleArray3 { public static void main(String args[]) { //DOUBLE ARRAY DECLARATION double a[], a2; //MEMORY ALLOCATION FOR DOUBLE ARRAY a = new double[4]; //ASSIGNING ELEMENTS TO DOUBLE ARRAY a[0] = 100000d; a[1] = 300000d; a[2] = 400000d; a[3] = 786777d; a2 = 333333d; //DOUBLE ARRAY OUTPUT System.out.println(); System.out.println("double Array Example"); System.out.println("=================="); System.out.println(); System.out.println("a2 value is : "+a2); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }

41

http://www.hudatutorials.com
In this example you can learn how to assign double array to other double array. /* double Array Example 4 */ /* Save with file name DoubleArray4.java */ public class DoubleArray4 { public static void main(String args[]) { //DOUBLE ARRAY DECLARATION double[] a, a2; //a AND a2 ARE ARRAY VARIABLES //MEMORY ALLOCATION FOR DOUBLE ARRAY a = new double[4]; //ASSIGNING ELEMENTS TO DOUBLE ARRAY a[0] = 100000d; a[1] = 300000d; a[2] = 400000d; a[3] = 786777d; //ASSIGNING a ARRAY TO a2 ARRAY VARIABLE a2 = a; //DOUBLE ARRAY OUTPUT System.out.println(); System.out.println("double Array Example"); System.out.println("=================="); System.out.println(); System.out.println("a array values"); System.out.println(); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } System.out.println(); System.out.println("a2 array values"); for(int i=0;i<a2.length;i++) { System.out.println("Element at Index : "+ i + " " + a2[i]); } } }

42

Sign up to vote on this title
UsefulNot useful