Introduction To Java: Subject Code CSE-404E
Introduction To Java: Subject Code CSE-404E
Topics to be covered
Introduction to Java, Data Types, Variables, Operators, Control Statements, Arrays
Features
C++ syntaxes simplified Cross Plate form Automatic Memory Management
No dangling pointers No memory leaks
No Make files/header files Object Oriented Rich with Powerful standard library
Why Bytecodes ?
Plate form Independent Load from the internet faster than source code A semi compiled java code is available for interpreter which has been debugged as well, at the compile time. Interpreter is faster and smaller than it would be for java source Source code is not revealed to end users Interpreter performs additional security checks, screen out machine code
Java
Java interpreter
Appletviewer
Tests applets without a browser
Javadoc
Generates HTML documentation (docs) from source
Jar
Package classes into jar files(packages)
Extcheck
Idlj Jar Jarsigner javah
Detects the version conflicts between a target JAR file and currently installed JAR file Generates the java bindings from a given IDL file
Combines multiple files into single jar file Signs jar files and verifies signatures on signed JAR files Facilitates implementation of java native methods
Javadoc
Monitoring and Jconsole,jps,jstat,jstatd management tools Troubleshooting Jinfo, jmap, jstack tools
Java Versions
Java 1.0 released in 1995 Java 1.1 released in 1997
A new event handling model based on listeners RMI and object serialization Support for inner and anonymous classes Arbitrary precision integers and floating-point numbers JDBC API for connecting to RDBMSs Java Beans Component architecture( an answer to ActiveX) Digitally signed applets to extend security privileges without resorting to the all or nothing model of browser plug-ins or active x
Output
Welcome to java programming
Command-Line Arguments
C:\javamethods\Ch02> javac Greetings.java C:\javamethods\Ch02> java Greetings Ramesh Kumar
Hello, Josephine Jaworski
public class Greetings { public static void main(String[ ] args) { String firstName = args[ 0 ]; String lastName = args[ 1 ]; System.out.println("Hello, " + firstName + " } }
Data Types
byte- 8 bits 1 byte short 16 bits 2 byte int 32 bits 4 byte long 64 bits 8 byte float-32 bits 4 bytes Double 64 bits 8 bytes char 16 bits 2 bytes boolean 1 bit
Java Integer Types
Access Modifiers
Access modifiers define varying levels of access between class members and the outside world( Other objects). For types of Access modifiers
default public protected private
Relational Operators
<, >, <=, >=, = =, !=
is equal to
is NOT equal to
Logical Operators
&&, ||, !
and
or not
Strings
String is not a primitive data type Strings work like any other objects, with two exceptions:
String in double quotes are recognized as literal constants + and += concatenate strings ( or a string and a number or an object, which is converted into a string)
Literal Constants
new line tab
char
int
double String
Symbolic Constants
Symbolic constants are initialized final variables:
private final int stepLength = 48; private static final int BUFFER_SIZE = 1024; public static final int PIXELS_PER_INCH = 6;
Arithmetic
Operators: +, -, /, * , % The precedence of operators and parentheses is the same as in algebra m % n means the remainder when m is divided by n (for example, 17 % 5 is 2; 2 % 8 is 2) % has the same rank as / and * Same-rank binary operators are performed in order from left to right
Arithmetic (contd)
The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in expressions. If one operand is an int and another is a double, the result is a double; if both operands are ints, the result is an int.
Arithmetic (contd)
The type of the result is determined by the types of the operands, not their values; this rule applies to all intermediate results in expressions. If one operand is an int and another is a double, the result is a double; if both operands are ints, the result is an int.
Arithmetic (contd)
To get the correct double result, use double constants or the cast operator:
double ratio = 2.0 / 3; double ratio = 2 / 3.0; int m = ..., n = ...; double factor = (double)m / (double)n; double factor = (double)m / n; double r2 = n / 2.0;
Casts
Arithmetic (contd)
A cast to int can be useful:
Returns a double
Arithmetic (contd)
Caution: the range for ints is from -231 to 231-1 (about -2109 to 2109)
Arithmetic (contd)
Compound assignment Increment and operators: decrement operators: a = a + b; a = a - b; a += b; a -= b; a = a + 1; a = a - 1; a++; a--;
a = a * b; a = a / b; a = a % b;
a *= b; a /= b; a %= b;
Initialization
Testing
Change
Initialization
Testing for (int p = 1; p < x; p *= 2) { Change n++; } return n; The scope of p is the body
}
The code runs through the body of the loop at least once
if condition is false, the next iteration is not executed
break in Loops
Example:
int d = n - 1; while (d > 0) { if (n % d == 0) break; d--; } if ( d > 0 ) // if found a divisor ...
break in Loops
Example:
int d = n - 1; while (d > 0) { if (n % d == 0) break; d--; } if ( d > 0 ) // if found a divisor ...
Arrays
What is an Array
An array is a block of consecutive memory locations that hold values of the same data type. Individual locations are called arrays elements. When we say element we often mean the value stored in that element.
1.39 1.69 1.74 0.0
An array of doubles
1.69
c[1]
1.74
c[2]
0.0 c[3]
c is arrays name
Indices (Subscripts)
In Java, an index is written within square brackets following arrays name (for example, a[k]). Indices start from 0; the first element of an array a is referred to as a[0] and the nth element as a[n-1]. An index can have any int value from 0 to arrays length - 1.
Indices (contd)
We can use as an index an int variable or any expression that evaluates to an int value. For example:
a [3] a [k] a [k - 2] a [ (int) (6 * Math.random()) ]
Indices (contd)
In Java, an array is declared with fixed length that cannot be changed. Java interpreter checks the values of indices at run time and throws ArrayIndexOutOfBoundsException if an index is negative or if it is greater than the length of the array - 1.
1000 times!
1000 times!
Arrays as Objects
In Java, an array is an object. If the type of its elements is anyType, the type of the array object is anyType[ ]. Array declaration:
anyType [ ] arrName;
Initialization (contd)
An array can be declared and initialized in one statement. For example:
int [ ] scores = new int [10] ; private double [ ] gasPrices = { 3.05, 3.17, 3.59 };
Initialization (contd)
Otherwise, initialization can be postponed until later. For example:
String [ ] words;
Not yet initialized
Arrays Length
The length of an array is determined when that array is created. The length is either given explicitly or comes from the length of the {} initialization list. The length of an array arrName is referred to in the code as arrName.length. length is like a public field (not a method) in an array object.
Initializing Elements
Unless specific values are given in a {} list, all the elements are initialized to the default value: 0 for numbers, false for booleans, null for objects. If its elements are objects, the array holds references to objects, which are initially set to null. Each object-type element must be initialized before it is used.
Two-Dimensional Arrays
2-D arrays are used to represent tables, matrices, game boards, images, etc. An element of a 2-D array is addressed using a pair of indices, row and column. For example:
board [ r ] [ c ] = 'x';
Dimensions (contd)
Java allows ragged arrays, in which different rows have different lengths. In a rectangular array, m[0].length can be used to represent the number of columns.
Ragged array: m.length Rectangular array: m.length
m[3].length
m[0].length
Triangular Loops
Transpose a matrix idiom:
int n = m.length; for (int r = 1; r < n; r++) { for (int c = 0; c < r; c++) { double temp = m [ r ][ c ]; m [ r ][ c ] = m [ c ][ r ]; m [ c ][ r ] = temp; } }
The total number of iterations through the inner loop is: 1 + 2 + 3 + ... + n-1 = n (n - 1) / 2
Questions ?
Thank You