Sie sind auf Seite 1von 9

EDUmobile M O B I L E T R A I N I N G

.org

Java – A beginner’s reference manual


Java is an object oriented programming language developed by James
Gosling and his team at Sun Microsystems. According to Wikipedia, “
The language derives much of its syntax from C and C++ but has a
simpler object model and fewer low-level facilities. Java applications are
typically compiled to bytecode (class file) that can run on any Java
Virtual Machine (JVM) regardless of computer architecture. Java is
general-purpose, concurrent, class-based, and object-oriented, and is
specifically designed to have as few implementation dependencies as
possible. It is intended to let application developers "write once, run
anywhere". Java is considered by many as one of the most influential
programming languages of the 20th century, and widely used from
application software to web application”

A) Developing a Java program involves

1. Writing code,
2. Compiling it into bytecode
3. Running the bytecode.

Your First Java Program( Refer to java intro video 1)

public class Hello {


public static void main(String[] args) {
System.out.println("Hello Java");
}
}

B) Variables are data place holders.

There are two data types in Java

1. Reference types provide a reference to an object.


2. Primitive types hold a primitive

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

public class Hello {


public static void main(String args[]) {
double a = 3.0, b = 4.0;

// c is dynamically initialized
double c = a * a + b * b;

System.out.println(" Operation results in " + c);


}
}

C) Constants are the values which do not change during the lifetime of
the program.

1. Using the final keyword to declare a variable.


2. The final keyword specifies that the value of a variable is final and
cannot be changed.
3. It is a convention in Java to write constants in uppercase letters.

public class Hello {

public static void main(String[] arg) {


final int FEET_PER_YARD = 3; // Constant values
final double MM_PER_INCH = 25.4; // that cannot be changed

System.out.println(FEET_PER_YARD);
System.out.println(MM_PER_INCH);

D) The Method main

1. A special method called main provides the entry point to an


application.

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

2. An application can have many classes and only one of the classes
needs to have the method main.
3. This method allows the class containing it to be invoked.

The signature of the method main is as follows.

public class MainClass {

public static void main(String[] args) {

1. In addition, you can pass arguments to main when using 'java' to


run a class.
2. To pass arguments, type them after the class name. Two
arguments are separated by a space.

java className arg1 arg2 arg3 ...

class CommandLine {
public static void main(String args[]) {
for (int i = 0; i < args.length; i++)
System.out.println("args[" + i + "]: " + args[i]);
}
}

E) The Primitive Types

Java has eight primitive types of data: byte, short, int, long, char, float,
double, and boolean.

These can be put in four groups:

1. Integers includes byte, short, int, and long


2. Floating-point numbers includes float and double
3. Characters includes char, like letters and numbers.
4. Boolean includes boolean representing true/false values.

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

byte

The smallest integer type


a range from -128 to 127.
useful when working with a stream of data from a network or file.
Byte variables are declared by use of the byte keyword.

byte b, c;

int

The most commonly used integer type


a signed 32-bit type
Ranging from -2,147,483,648 to 2,147,483,647
used to control loops and to index arrays.
the most efficient type

long

a signed 64-bit type

Type Default Value


Boolean - false
Byte - 0
Short - 0
Int - 0
Long - 0L
Char - \u0000
Float - 0.0f
Double - 0.0d
Object reference - null

Sample Program
public class ClassInitializer1 {
static boolean bool;
static byte by;
static char ch;
static double d;
static float f;
static int i;
static long l;

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

static short sh;


static String str;

public static void main(String[] args) {


System.out.println("bool = " + bool);
System.out.println("by = " + by);
System.out.println("ch = " + ch);
System.out.println("d = " + d);
System.out.println("f = " + f);
System.out.println("i = " + i);
System.out.println("l = " + l);
System.out.println("sh = " + sh);
System.out.println("str = " + str);
}
}

F) In Java, there are six categories of operators.

1. Unary operators
2. Arithmetic operators
3. Relational and conditional operators
4. Shift and logical operators
5. Assignment operators
6. Other operators

Precedence Operator Description Association


1 ++,-- Postincrement, Postdecrement R -> L
2 ++,-- Preincrement, Predecrement R -> L
+,- Unary plus, unary minus R -> L
~ Bitwise compliment R -> L
! Boolean NOT R -> L
3 new Create object R -> L
(type) Type cast R -> L
4 *,/,% Multiplication, division, remainder L -> R
5 +,- Addition, subtraction L -> R
+ String concatenation L -> R
6 <<, >>, >>> Left shift, right shift, unsigned right shift L -> R
7 <, <=, >, >= L -> R
instanceof Type comparison L -> R
8 ==, != Value equality and inequality L -> R
==, != Reference equality and inequality L -> R

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

9 & Boolean AND L -> R


& Bitwise AND L -> R
10 ^ Boolean XOR L -> R
^ Bitwise XOR L -> R
11 | Boolean OR L -> R
| Bitwise OR L -> R
12 && Conditional AND L -> R
13 || Conditional OR L -> R
14 ?: Conditional Ternary Operator L -> R
15 =,+=,-=, Assignment Operators R -> L
*=,/ =,%=,
&=,^=, |=,
<<=, >> =,
>>>=

Operator
Operator Precedence Group Associativity
Precedence
(), [], postfix ++, postfix -- left Highest
unary +, unary -, prefix ++, prefix --, ~,
right
!
(type), new left
*, /, % left
+, - left
<<, >>, >>> left
< ,<= , >, >=, instanceof
==, !=
& left
^ left
| left
&& left
|| left
?: left
=, +=, -=, *=, /=, %=, <<=, >>=, >>>=,
right lowest
&=, |=, ^=

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

G) An Overview of Java Statements

1. In programming, a statement is an instruction to do something.


2. It controls the sequence of execution of a program.
3. In Java, a statement is terminated with a semicolon and multiple
statements can be written on a single line.

x = y + 1; z = y + 2;

In Java, an empty statement is legal and does nothing:

The if statement syntax

The if statement is a conditional branch statement. The syntax of the if


statement is either one of these two:

if (booleanExpression) {
statement (s)
}

or

if (booleanExpression) {
statement (s)
} else {
statement (s)
}

public class Hello {

public static void main(String[] args) {


int a = 3;
if (a > 3) {

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

// statements
} else {
// statements
}
}

Switch Case example

public class Hello {


public static void main(String[] args) {
int choice = 2;

switch (choice) {
case 1:
System.out.println("Choice 1 selected");
break;
case 2:
System.out.println("Choice 2 selected");
break;
case 3:
System.out.println("Choice 3 selected");
break;
default:
System.out.println("Default");
break;
}
}
}

H) Loops in Java

The while Statement

1. One way to create a loop is by using the while statement.


2. Another way is to use the for statement

Copyright © EDUmobile.org All Rights Reserved


EDUmobile M O B I L E T R A I N I N G
.org

The while statement has the following syntax.

while (booleanExpression) {
statement (s)
}

1. Statement(s) will be executed as long as Boolean Expression


evaluates to true.
2. If there is only a single statement inside the braces, you may omit
the braces.
3. For clarity, you should always use braces even when there is only
one statement.

As an example of the while statement, the following code prints integer


numbers that are less than three.

public class MainClass {

public static void main(String[] args) {


int i = 0;
while (i < 3) {
System.out.println(i);
i++;
}
}

For loop

Syntax
For( initializer; condition; increment/decrement)

for(int i=0; i<100;i++) or for(int i=100; i>0;i--)

Refer to video java intro 3 for live demo.

Copyright © EDUmobile.org All Rights Reserved

Das könnte Ihnen auch gefallen