SE101 Lec1 JAVABASICS
SE101 Lec1 JAVABASICS
3
Object Oriented Programming using Java
General Module Aims
Those source files are then compiled into .class files by the
javac compiler.
*/
package helloworldapp;
/**
* The HelloWorldApp class
* simply prints "Hello World!" to standard output.
*/
public class HelloWorldApp {
}
Identify:
Package declaration
Class Declaration
Starts with a Capital letter.
Method Declaration
Comments (3 types)
Source Code Comments
Comments are ignored by the compiler but are useful to other
programmers. The Java programming language supports three
kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for
short). The compiler ignores this kind of comment, just like it
ignores comments that use /* and */.
The javadoc tool uses doc comments when preparing automatically
generated documentation.
// text
The compiler ignores everything from // to the end of the
line.
The HelloWorldApp Class Definition
As shown above, the most basic form of a class definition is:
class ClassName {
...
}
The keyword class begins the class definition for a class named
name, and the code for each class appears between the opening
and closing curly braces marked in bold above.
You can name the argument anything you want, but most
programmers choose "args" or "argv".
Hello World!
Java is case-sensitive!
This means that a variable named as Count, for
example, would NOT be the same as a variable
named as count or COUNT.
class names – same as for variable names but with the first
letter as a capital, e.g. Person, Car, User.
keywords (i.e. those that represent statements,
like if and while, and data types) – always in lower
case – you have no choice. If you use the wrong
case for a keyword it will not be recognised!
text=text+"has failed
Using comments:
Introduce every program with a comment - giving name, date written,
outline of purpose.
Add comments to your code as you write it (so that later on you will
remember why you did it that way!).
For example:
5+3 8
Operator
Arithmetic
Increment & Decrement
Assignment
Shorthand
Comparison
Relational
Variables:
Declaration: To declare a variable, give the type,
then one or more spaces, then the name, then a
semicolon.
int total;
String name;
double rate;
Symbolic constants
The keyword final is used, and the name is all capitals:
final int MAXRECORDS = 100;
Casting
Used to carry out temporary conversion of a value to another type,
for example converting an integer type to a floating point type in
order to avoid integer division.
e.g.
average = sum / (float)noOfValues;
int num1 = 4;
int num2 = 7;
num2 / num1
num2 / 2
num2 / 2.0
(double)num2 / 2
(double)num2 / num1
(double)num2 / (double)num1
num2 / (double)num1
int num1 = 4;
int num2 = 7;
(double)num2 / num1
(double)num2 / (double)num1
num2 / (double)num1
Decision-making statements
(if-then, if-then-else, switch)
Branching statements
(break, continue, return)
Looping statements
(for, while, do-while, for each)
Ex:
// Class will dispaly the function of a while Loop
Syntax:
for(declaration : expression){
//statement to be print
}
Ex:
// Class will dispaly the function of a Enhance For Loop
class TestEnhanceForLoop {
public static void main(String args[]){
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
System.out.print("\n");
for(int x : numbers ){
if( x == 30 ){
break;
}
System.out.print( x );
System.out.print("\n");
}
System.out.print(“I’m out of the Loop
now");
}
Result:
10
20
Now Out of the Loop
Ex.
// Class will display the function of a Continue
statement
for(int x : numbers ){
if( x == 30 ){
continue;
}
System.out.println( x );
}
}
}
Decision-making statements
(if-then, if-then-else, switch)
Ex:
// Class will dispaly the function of a if Loop
if( x < 20 ){
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
}
}
Ex:
public class TestIfElseIfCondition {
public static void main(String args[]){
int x = 30;
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else statement");
}
}
}
Ex:
public class TestNestedIfCondition {
public static void main(String args[]){
int x = 30;
int y = 10;
if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
}
Ex:
public class Test {
public static void main(String args[]){
char grade =‘A’;
switch(grade)
{
case 'A' :
System.out.println("Excellent!");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
Result:
Excellent!
Your grade is a A
NEXT CLASS:
Methods
Array/ String
OOP