[Go to site: main page, start]

0% found this document useful (0 votes)
5 views5 pages

Java Program Basics and Syntax Guide

Jave notes

Uploaded by

ajuna
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)
5 views5 pages

Java Program Basics and Syntax Guide

Jave notes

Uploaded by

ajuna
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

Simple Java Program

This program will allow you to understand the layout to Java Programs:
// Text printing program
public class Welcome {
// main method begins execution of Java application
public static void main(String[] args) {
for (int i = 0; i < 3; i++)
[Link]("Welcome to SUPCOM");
} // end method
} // end class Welcome

Compiling, and Executing a Java Program


Sun releases each version of J2SE with a Java Development Toolkit (JDK).

Figure1.1 ( process diagram)


Methods:
Syntax
[modifiers] data-type method-name ([parameter-declaration]) {…}

Calling a Method
When creating a method, you give a definition of what the method is supposed to do.
There are two types of methods void methods and value methods.

Void method example:


public static void printText () {
[Link]("Welcome to Java!");

}// end method

Value method example:


public static int add (int a, int b) {
return a+b;
}// end method

To use a method, you have to call or invoke it. There are two ways to call a method; the
choice is based on whether the method returns a value or not:
1. If the method returns void it should be called using a statement. For example,
method-name (passed-arguments);
We can call the above-mentioned void method using the following statement
printText();

2. If the method returns a value, a call to the method is usually treated as a


value. For example:
data-type variable-name = method-name (passed-arguments);
Calls method-name (passed-arguments) and assigns the result of the
method to the variable variable-name.
Another example of a call that is treated as a return-value is:
[Link](method-name(passed-arguments));
Which prints the return value of the method call method-name (passed-
arguments)
We can call the above-mentioned value method using the following
statements:
int result = add (5,8);

or

[Link](add (5,8));

Conditional Statements in Java


Check for certain conditions, for the program to make decision. You need if statement and
operators for expressing logic.
• Relational operators

x == y // x is equal to y
x != y // x is not equal to y
x>y // x is greater than y
x<y // x is less than y
x >= y // x is greater than or equal to y
x <= y // x is less than or equal to y

The result of a relational operator is one of two special values, true or false. These values
belong to the data type boolean; in fact, they are the only boolean values.

• Logical operators
Java has three logical operators: &&, ||, and !, which respectively stand for and, or,
and not. The results of these operators are similar to their meanings in English.
• Note: (=) is assignment operator, (==) is comparison operator

1. Single Condition
if (expression) {
statement(s);
}

2. Single Condition with else


if (expression) {
statement(s);
}
else {
statement(s);
}
3. Multiple conditions:
if (expression) {
statement(s);
}
else if (expression) {
statement(s);
}

else if (expression) {
statement(s);
}

else {
statement(s);
}

Control Structures (Loops):


1. while loop:
while (expression) {
Statement(s);
}

2. do-while loop:
do {
Statement(s);
} while (expression);

3. for loop :
for (initial-action; expression ;action-after-each-iteration) {
Statement(s);
}
Arrays
1. To use an array in a program, you must declare a variable to reference the array, and
you must specify the type of array the variable can reference.

dataType[ ] arrayName = new dataType[sizeOfAnArray];

dataType arrayName[ ] = new dataType[sizeOfAnArray];

Examples:

int [] counts = new int [10];


String[] names = {"Andersson", "Johansson", "Nordin",
"Holmgren"}

2. To pass an array to a method as a parameter you have to write the following:


modifiers data-type method-name (dataType [ ]arrayName) {…}

You might also like