Lec 1 Fundamentals Feb 23
Lec 1 Fundamentals Feb 23
Lec 1 Fundamentals Feb 23
CS212-Object Oriented
Programming
Lecture 1
Java Language Fundamentals
(Feb 2023)
Instructor: Lt Col Muhammad Imran Javaid
Email: imranjavaid@mcs.edu.pk
2
Data Types
• Adata type describes the nature of data that a variable or expression can
contain/represent
• Our computers use memory which is
• limited in size and
• ultimately consists of bits (binary values 0 and 1 )
• Therefore, all data that we can represent must be
• limited in size and
• ultimately be broken down to bits
• In Java, we can distinguish different sorts of data types:
• Boolean Types: represent a Boolean decision of true and false
boolean myBool = true;
• Integer Types: represent whole numbers, i.e., subsets of 6, int myNum = 5;
• Floating Point Types: represent fractional numbers float myFloatNum = 5.99f;
• Char Types: represent a character “a” char myLetter = 'D';
String myText = "Hello";
3 • String Types: represent a text e.g “hello”
Data Types
• In Java, we can process data of several basic primitive data types
4
Variables
• A variable is a container which has a specific data type and can store
exactly one value of that type
• Variables are declared with statements of the form [type] [variableName];
which creates a variable with name variableName and of type type .
• We can store a value in a variable by using statements of the form
[variableName] = [expression] , where variableName is the name of the
variable and expression must be an expression of the right type
• When a program is executed, variables exist in the RAM assigned to the
process. After the process has terminated, they disappear.
5
Numbers
• Numbers are always represented relative to a given base
• The least significant/important digit is always the right-most one whereas
the one with the highest value is on the left side
• We usually use numbers relative to base 10:
– 1234 means
• Binary numbers are relative to base 2 and written in the form of
0b... in Java
– 0b1234 is invalid, since only digits 0 and 1 can occur
– 0b10100 means,
• Hexadecimal numbers are relative to base 16 (using digits 0 . . . 9, a. . .f)
and written in form 0x... in Java
– 0x1234 means
– 0x10100 means
6
Assignment Operators
Operator Example Same As
= 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
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
7
Comparison Operators
!= Not equal x != y
8
Logical Operators
9
Ternary Operator
10
Expressions
• Integer arithmetic is exact, i.e. ((a − b) − a) + b = 0, but since we have only 8, 16, 32, or 64 bits, the range
of numbers we can represent is limited. Thus, if, e.g., a + b is outside of the range of numbers we can
11 represent, it will be “wrapped back in”, i.e., we get the wrong result
Strings
• A String variable contains a collection of characters surrounded by
double quotes e.g String txt = "Hello World";
String txt = "Please locate where 'locate' occurs!"; // Find Character in String
System.out.println(txt.indexOf("locate")); // Outputs 7
12
Escape Sequences
• The backslash (\) escape character turns special characters into string
characters:
String txt = “We from “Pakistan” “; // ERROR
String txt = “We from \“Pakistan\” “; // Outputs We are from “Pakistan”
28
Primitive Data Types
• There are eight primitive data types supported by Java. Primitive data
types are predefined by the language and named by a keyword.
Numerical Range
Keyword Bytes
Minimum Maximum
32
Java Console Input/Output
Output to File
• If a program writes output to the console, then this output can be written to a
file instead. // Java program to demonstrate redirection in System.out.println()
import java.io.*;
}
33
}
Java Console Input/Output
Input
• System.in is an InputStream which allows us to write read single
characters from the console
• We can get this by wrapping System.in into a Scanner object scanner by
doing Scanner input = new Scanner(System.in);
• Well, we did not yet learn what an object is and what new does, so let us
ignore this aspect for now and focus just on reading data
– input.nextLine() reads a full line of text as a String
– input.nextInt() reads an int number (from text)
– input.nextDouble() reads a double number (from text)
– input.hasNext() check if there is something else to read
34 • But before using scanner , import it first import java.util.Scanner; // Import the Scanner class
Scanner Input
• Scanner input = new Scanner(System.in);
– boolean hasValue = input.nextBoolean();
36
Comparison Operators
37
Expressions
38
Quadratic Equation
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
double a, b, c, root, x1, x2;
Scanner input = new Scanner(System.in);
System.out.print("Enter a: ");
a = input.nextDouble();
System.out.print("Enter b: ");
b = input.nextDouble();
System.out.print("Enter c: ");
c = input.nextDouble();
root = Math.sqrt(Math.abs(Math.pow(b, 2)
- 4 * a * c));
x1 = (-b + root) / (2 * a);
x2 = (-b - root) / (2 * a);
System.out.printf("X1 = %.2f\n", x1);
System.out.printf("X2 = %.2f\n", x2);
}
39 }
Quadratic Equation
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
double a, b, c, root, x1, x2;
Scanner input = new Scanner(System.in);
System.out.print("Enter a: ");
a = input.nextDouble();
System.out.print("Enter b: ");
b = input.nextDouble();
System.out.print("Enter c: ");
c = input.nextDouble();
root = Math.sqrt(Math.abs(Math.pow(b, 2)
- 4 * a * c));
x1 = (-b + root) / (2 * a);
x2 = (-b - root) / (2 * a);
System.out.printf("X1 = %.2f\n", x1);
System.out.printf("X2 = %.2f\n", x2);
}
40 }
Copyright Notice
41