Lec 1
Lec 1
Outline
• Java Quickstart (print – println - printf) methods
• Java Comments
• Java Data Types
• JAVA Conditions if else-if – shorthand if...else
• Loops ( while – do while – for)
• Exercises
• Procedural vs Object Oriented Programming OOP
• Java Operators
1
Java Quickstart
In Java, every application begins with a class name, and that class must match the filename.
Let's create our first Java file, called Main.java, which can be done in any text editor (like Notepad).
The file should contain a "Hello World" message, which is written with the following code:
System.out.println("Hello World");
Note: The curly braces {} marks the beginning and the end of a block of code.
System is a built-in Java class that contains useful members, such as out, which is short for "output".
The println() method, short for "print line", is used to print a value to the screen (or a file).
Don't worry too much about System, out and println(). Just know that you need them together to
print stuff to the screen.
You should also note that each code statement must end with a semicolon (;).
You can add as many println() methods as you want. Note that it will add a new line for each method:
Example
System.out.println("Hello World!");
System.out.println("It is awesome!");
2
The print() method
The only difference is that it does not insert a new line at the end of the output:
Example
Example
System.out.println(3);
System.out.println(358);
System.out.println(50000);
You can also perform mathematical calculations inside the println() method:
System.out.println(3 + 3); → 6
System.out.println(2 * 5); → 10
printf() Method
The printf() method outputs a formatted string.
Example
3
Java Comments
Comments can be used to explain Java code, and to make it more readable. It can also be used
to prevent execution when testing alternative code.
Single-line Comments
Any text between // and the end of the line is ignored by Java (will not be executed).
Example
// This is a comment
System.out.println("Hello World");
Example
This example uses a multi-line comment (a comment block) to explain the code:
Example
System.out.println("Hello World");
4
Java Data Types
Example
• Primitive data types - includes byte, short, int, long, float, double, boolean and char
• Non-primitive data types - such as String, Arrays and Classes
a. Primitive Data Types
A primitive data type specifies the size and type of variable values, and it has no additional methods.
byte 1 byte Stores whole numbers from -128 to 127 (-2n-1 to 2n-1 – 1)
n : No of bits
5
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
- The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
Example
System.out.println(myGrade);
Output: B
Alternatively, if you are familiar with ASCII (American Standard Code for Information
Interchange) values, you can use those to display certain characters:
Example
System.out.println(myVar1);
6
System.out.println(myVar2);
System.out.println(myVar3);
Non-primitive data types are called reference types because they refer to objects.
The main difference between primitive and non-primitive data types are:
• Primitive types are predefined (already defined) in Java. Non-primitive types are created
by the programmer and is not defined by Java (except for String).
• Non-primitive types can be used to call methods to perform certain operations, while
primitive types cannot.
• A primitive type has always a value, while non-primitive types can be null.
• A primitive type starts with a lowercase letter, while non-primitive types starts with an
uppercase letter.
• The size of a primitive type depends on the data type, while non-primitive types have all
the same size.
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size type:
Example
int myInt = 9;
System.out.println(myInt); // Outputs 9
7
JAVA Conditional Statements -if Statement:
Syntax
if (condition) {
Example
public class Main {
public static void main(String[] args) {
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
}
}
8
Short Hand if...else:
variable = (condition) ? expressionTrue : expressionFalse ;
Example
public class Main {
public static void main(String[] args) {
int time = 20;
String result;
result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);
}
}
Example
public class Main {
public static void main(String[] args) {
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
9
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}
}
10
Do/While Loop
public class Main {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
}
}
output
0
1
2
3
4
For Loop:
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
output
0
1
2
3
4
11
Nested Loops
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 2; i++) {
System.out.println("Outer: " + i);
for (int j = 1; j <= 3; j++) {
System.out.println(" Inner: " + j);
}
}
}
}
output
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3
Exercises:
1. Write a Java program that reads a floating-point number and prints "zero" if the number
is zero. Otherwise, print "positive" or "negative". Add "small" if the absolute value of the
number is less than 1, or "large" if it exceeds 1,000,000.
Test Data
Input a number: 25
Expected Output :
Input value: 25
Positive number
2. Write a Java program to display n terms of natural numbers and their sum.
[Input number: 3
The first n natural numbers are : 3
1
2
3
12
The Sum of Natural Number upto n terms : 6 ]
Method The main program gets divided into It involves the concept of classes
minute parts on the basis of the and objects. Hence, it divides
functions. It then treats them as the program into minute chunks
separate programs for smaller known as objects. These are
programs individually. actually instances of classes.
13
Importance This programming model does not This programming model gives
give importance to data. It prioritizes importance to the data rather
the functions along with the than functions or procedures. It
sequence of actions that needs to is because it works on the basis
follow. of the real world.
Type of It divides any large program into It divides the entire program
Division small units called functions. into small units called objects.
14
Modes of The Procedural Programming offers The Object Oriented
Access no specific accessing mode for Programming offers three
accessing functions or attributes in a accessing modes- protected,
program. private, and public. These, then,
serve as a share to access
functions of attributes.
Size of It is not very suitable for solving any It is suitable for solving any big
Problems big or complex problems. or complex problems.
Addition of It is not very easy to add new It is very easy to add new
New functions and data in the Procedural functions and data in the Object
Function and Programming. Oriented Programming.
Data
Data Sharing It shares the global data among the It shares data among the objects
functions present in the program. through its member functions.
Data Hiding No proper way is available for hiding It can hide data in three modes-
the data. Thus, the data remains protected, private, and public. It
insecure. increases the overall data
security.
15
Examples Some common examples of The examples of Object
Procedural Programming are C, Oriented Programming
Fortran, VB, and Pascal. languages are Java, C++, VB.NET,
Python, and C#.NET.
Java Operators
Java divides the operators into the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Arithmetic Operators
16
++ Increment Increases the value of a variable by 1 ++x
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
17
Java Comparison Operators
Operator Name Example
== Equal to x == y
!= Not equal x != y
Logical
&& Returns true if both statements are true x < 5 && x < 10
and
18
Example:
a = 10; c %= a = 5
c = 15; c <<= 2 = 20
c %= a ;
System.out.println("c %= a = " + c ); c >>= 2 = 5
c >>= 2 = 1
c <<= 2 ;
System.out.println("c <<= 2 = " + c ); c &= a = 0
c >>= 2 ; c ^= a = 10
System.out.println("c >>= 2 = " + c ); c |= a = 10
c >>= 2 ;
System.out.println("c >>= 2 = " + c );
c &= a ;
System.out.println("c &= a = " + c );
c ^= a ;
19
System.out.println("c ^= a = " + c );
c |= a ;
System.out.println("c |= a = " + c );
}
}
20