1

CS111: PROGRAMMING LANGUAGE II Computer Science Department

Lecture 1(b): Java Basics (I)

Lecture Contents 2



Java basics  Input/output  Variables

 Expressions

Computer Science Department

Your first Java program.. 3

// indicates a comment. class keyword Java is case sensitive braces { , } delimit a class body main Method

“Everything must be in a class” There are no global functions or global data. Computer Science Department

Basic language elements 4

Text I/O 5



Standard output.  Flexible

OS abstraction for output.  In Java, applications use the standard output object (System.out) to display text on terminal.

Computer Science Department

Command line output 6

Multiple line O/P Formatting output

public class TestIO { public static void main(String[] args) { System.out.println(“Welcome to java”); System.out.println(“Welcome to \n java”); System.out.print(“Welcome to”); System.out.println(“java”); System.out.printf(“%s\n%s\n“, “Welcome to”,”java”); }

} } Computer Science Department

Common escape sequences 7

Computer Science Department

Printf Conversion-Characters 8

Computer Science Department

Strings & Text 9

String msg1 = new String( “Hello” ); String msg2 = “Hello” ; String msg3 = “Year “ + 2005; //valid?? Computer Science Department

Standard Input 10

 

input is received from Terminal window. Input entered while program is executing.

Computer Science Department

Reading values 11

 

use : import java.util.Scanner; Define an object of the Scanner class: Scanner input = new Scanner(System.in);



input values: num1 = input.nextInt();



Display after calculation: System.out.printf(“the square is : %d \n”, num1*num1);

Computer Science Department

example 12

Computer Science Department

example 13

Computer Science Department

variables 14



Declaration, memory allocation, initialization data type

variable name

int total = 0; int count, temp, result; Multiple variables can be created in one declaration

Computer Science Department

Constants 15





A “constant variable” is an identifier that is similar to a variable except that it holds one value for its entire existence Why constants:   



give names to otherwise unclear literal values facilitate changes to the code prevent inadvertent errors

In Java: final double PI = 3.14159265;

Computer Science Department

Arithmetic Expressions 16

 

An expression is a combination of operators and operands Arithmetic expressions (we will see logical expressions later) are essentially special methods applied to numerical data objects: compute numeric results and make use of the arithmetic operators: Addition Subtraction Multiplication Division Remainder

+ * / % Computer Science Department

Assignment-related Operators 17

 

Increment and decrement operators: ++, -Assignment operators: +=, -=, *=, /= these three expressions have the same effect

count

=

count + 1;

count

+= 1;

count

++;

these two expressions have the same effect

count

=

count - 10;

count

-= 10; Computer Science Department

Operator Precedence 18



What is the order of evaluation in the following expressions? a + b + c + d + e 1 2 3 4

a + b * c - d / e 3 1 4 2

a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1 Computer Science Department

How Do Data Conversions Happen? 19



Implicitly:  occurs

automatically  uses widening conversion,  Examples

:

4.0 / 8 (which / is it: double/double, float/float, int/int) 4 / 8.0 (which / is it: double/double, float/float, int/int) 4 + 5 / 9 + 1.0 + 5 / 9 / 10.0 (what is the value?)

Computer Science Department

How Do Data Conversions Happen? 20



Explicitly: Casting  widening

/ narrowing conversions  Examples: double MyResult; MyResult = 12.0 / 5.0; //OK int myInt = (int) MyResult; // truncation MyResult = (double)myInt/3.0;

Computer Science Department

Be careful!! 21



Example: in 1996, Ariane 5 rocket exploded after takeoff because of bad type conversion.

Computer Science Department

Class Math 22



All Math class methods are static 



Each is called by preceding the name of the method with the class name Math and the dot (.) separator

Method arguments may be constants, variables or expressions

Computer Science Department

23

Computer Science Department

Example 24



Solve quadratic equation ax2 + bx + c = 0. public class Quadratic { public static void main(String[] args) { double a,b,c,d; // input coefficient values.. // calculate roots d = Math.sqrt(b*b - 4.0*a*c); double root1 = (-b + d) / (2.0*a); double root2 = (-b - d) / (2.0*a); // print them out System.out.println(root1); System.out.println(root2); } } Computer Science Department

25

That’s all for today….. Text Book: Chap 2

Computer Science Department