Introduction To Java: BY Umar Zia Iimt I.E.T (Computer Science)
Introduction To Java: BY Umar Zia Iimt I.E.T (Computer Science)
Introduction
Present the syntax of Java Introduce the Java API Demonstrate how to build
o o
stand-alone Java programs Java applets, which run within browsers e.g. Netscape
Example programs
Why Java?
Its the current hot language Its almost entirely object-oriented It has a vast library of predefined objects and operations Its more platform independent
o
HelloWorld (standalone)
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }
Note that String is built in println is a member function for the System.out class
Control statements II
switch (n + 1) { case 0: m = n - 1; break; case 1: m = n + 1; case 3: m = m * n; break; default: m = -n; break; }
Java also introduces the try statement, about which more later
Java isn't C!
In C, almost everything is in functions In Java, almost everything is in classes There is often only one class per file There must be only one public class per file The file name must be the same as the name of that public class, but with a .java extension
What is a class?
Early languages had only arrays
o
Name conventions
Java is case-sensitive; maxval, maxVal, and MaxVal are three different names Class names begin with a capital letter All other names begin with a lowercase letter Subsequent words are capitalized: theBigOne Underscores are not used in names These are very strong conventions!
An example of a class
class Person { String name; int age; void birthday ( ) { age++; System.out.println (name + ' is now ' + age); } }
An array is an object
Person mary = new Person ( ); int myArray[ ] = new int[5];
o
or: