You are on page 1of 6

CHAPTER 2

JAVA PROGRAMMING BASICS

2.1 Structured Language vs OOP language (C++ vs Java)


- Please find out?

2.2 Introduction to Java Application

- java application programs are stand-alone programs.


Example : java applications program

public class Welcome


{
public static void main(Strings[] args)
{
System.out.println(“Welcome to Java Programming”);
}
}

- programming language’s rules, symbols, and special words enable you


to write programs to solve problems.
- Token is the smallest individual unit of a program written in any programming
language. Java’s token are divided into special symbols, word symbols and
identifiers.

Special Symbols
- some of the special symbols:
+ - * /
. ; ? ,
<= != == >=

Word Symbols (reserved words or keywords)


- some word symbols:
int, float, double, char, void, public, static, throws, return
- all letters in reserved word are always lowercase.

Identifiers
- names of things such as variables, constants, and methods.
- Some identifiers are predefined (example: print, println, printf –
output and nextInt, nextDouble, next, nextLine – input data),
others are defined by the user.
- All identifiers must obey Java’s rules. It can consists of letters, digits, the
underscore character(_) and dollar sign ($), and must begin with letter,
underscore, or the dollar sign.
- Java is case sensitive – uppercase and lowercase letters are considered
different.
- Can be any length.

1
2.3 Data Types

- a set of values together with a set of operations.


- Two types: primitive and objects.

Primitive

2 types:
i- Numerical data type
ii. Constant data type

Numerical Data Type

- the fundamental data types in Java.


- Three categories:
~ Integral: integers, or numbers without a decimal part.
~ Floating-point : deal with decimal numbers.
~ Boolean: deals with logical values.
- Integral : char, byte, short, int, long

Data Type Values Storage(in bytes)


char 0 to 65535 2 (16 bits)
byte -128 to 127 1 (8 bits)
short -32768 to 32767 2 (16 bits)
int -2147483648 to 2147483647 4 (32 bits)
long -922337203684547758808 to 8 (64 bits)
922337203684547758807
Values and Memory Allocation for Integral Data Types
Example:
int num;
long y = 12345678 ;
char ch = ‘a’ ;

- Floating-point : float and double

Data Type Values Storage(in bytes)


float -3.4E+38 to 3.4E+38 4
double -1.7E+308 to 1.7E+308 8
Note: maximum number of decimal places – float : 6 or 7 ; double : 15

Example:
float num;
double y = 12.456789;

- Boolean: true or false.

Example:
boolean student;
student = true;

2
Constant data type

- declared using the keyword final.

Example:
final int SIZE = 40;
- constant inside a class are declared with the keyword static.

Example:
public static final int SIZE = 40;

Object data type: String

- String is a sequence of zero or more characters. In Java, string are enclosed


in double quotation marks.
Example
String name = “Nik Husna”;
- is a class in java.lang packages.
- String that contains no characters is called a null or empty string.
Example
String book = “”;
- every character has a relative position. First character at position 0, second
character at position 1 and so forth.
- length of a string is the number of characters in it.
Example:
“Nik Husna” position of N is 0
position of i is 1
position of k is 2
position of a space is 3
position of H is 4
position of u is 5
position of s is 6
position of n is 7
position of a is 8

- String manipulation.

Example:
String look = “Ameera is pretty”;
String see, sub1, sub2;
int n;
see = look.toUpperCase(); //”AMEERA IS PRETTY”
see = look.toLowerCase (); //”ameera is pretty”
n=look.length(); //16
char x =look.charAt(4); //r
sub1 = look.subString(7); // is pretty
sub2 = look.subString(7,9); // is
see = look.replace(“Ameera”,”Hanna”);
//Hanna is pretty

3
2.4 Control Structures

- two types
i. Decision statement
ii. Iteration statement
Decision statement

i. if statement

syntax:
if (condition)
{
statements;
}
~ statments will be implemented only when the condition is true.
Example:
if ( age < 7)
{
System.out.println(“go to kindergarten”);
}

ii. if – else statement

syntax:
if (condition)
{
Statement_1;
}
else {
Statement_2;
}

~ statement_1 will be implemented only when the condition is true. If the


condition is false, statement_2 will be implemented.
Example:
if ( age < 7)
{
System.out.println(“go to kindergarten”);
}
else
{
System.out.println(“go to school”);
}

iii. nested if statement (multiple selection)

syntax:
if (condition_1)
{
Statement_1;
}

4
else if (condition_2)
{
Statement_2;
}
else …
:
:
else if (condition_n)
{
Statement_n;
}
else
Statement_m;

iv. switch..case statement

Iteration statement

~ while loop
~ do..while loop
~ for loop
~ nested loop
~ break and continue statement

break

~ purposely used for:


 to exit early from a loop
 to skip the remainder of the switch structure
~ when the break statement executes, the program will execute starting at the
first statement after the structure.
~ example:
for(int k=0; k<5;k++)
if (k%2 == 0)
break;
in this loop, if the k%2 is equal to 2 the loop will be terminated although the
value k is not yet reach 5.

continue
~ used in while, for and do..while structures.
~ when it is executed in a loop, it skips the remaining statements in the loop and
proceeds with the next iteration of the loop.

2.5 Array

• An array is a collection of data values.


• If your program needs to deal with 100 integers, 500 Account objects, 365 real
numbers, etc., you will use an array.
• In Java, an array is an indexed collection of data values of the same type.

5
2.5.1 Array declaration of Primitive Data Types

• Array Declaration
<data type> [ ] <variable> //variation 1
<data type> <variable> [ ] //variation 2
• Array Creation
<variable> = new <data type> [ <size> ]
• Example
double[ ] rainfall;
rainfall = new double[12];

double rainfall[ ];
rainfall = new double[12];

2.5.2 Accessing Individual Elements


Individual elements in an array accessed with the indexed expression.
The index of the first position in an array is 0.

double[] rainfall = new double[12];

rainfall[2] ???

2.5.3 Array Processing – Sample1

Scanner scanner = new Scanner(System.in);


double[] rainfall = new double[12];
double annualAverage, sum = 0.0;
for (int i = 0; i < rainfall.length; i++)
{
System.out.print("Rainfall for month " + (i+1));
rainfall[i] = scanner.nextDouble( );
sum += rainfall[i];
}
annualAverage = sum / rainfall.length;

2.5.4 Array Initialization

• Like other data types, it is possible to declare and initialize an array at the same
time.
• Example:

int[] number = { 2, 4, 6, 8 };
double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2,
9.00, 3.123, 22.084, 18.08 };
String[] monthName = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"};

You might also like