CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Programming in JAVA II (A Whirlwind Tour of Java) Course Lecture Slides 17 May 2010 Ganesh Viswanathan
JAVA Java 1.0 was first released by Sun Microsystems in 1995. Duke, the JAVA mascot Java became popular for web programming. "Write Once, Run Anywhere" (WORA) philosophy. Key design principles: - Platform independence - Security - Stability
How Java works!
Programming in JAVA I Basic concepts in Java Values Variables Complex data/objects Methods Expressions Conditionals Loops
Programming in JAVA English to JAVA translation: Piece of data: value Kind of data: type Place to store a value: variable Structured group of variables: object Kind of object: class Object of a particular class: class instance Variable belonging to an object: instance variable
Programming in JAVA Values and Types: All data values in Java have a type The type of a value determines: Allowed operations Memory representation Allowed operations Conversion to other types Allowed operations Types are program invariants Help to catch errors Default initial values (if omitted) are based on type
Programming in JAVA Primitive Types: EIGHT basic pre-defined types: values on which Java can operate directly. byte, short, int, long, float, double, boolean, char Plus, special support for character strings - using java.lang.string class
Programming in JAVA - II Numeric Types in Java are characterized by their size (how much memory they occupy) Integral types Floating point types Size Range (inclusive) byte 1 byte -128: 127 short 2 bytes -32,768: 32,767 char 2 bytes 0: 65,535 int 4 bytes -2 31 : 2 31-1 long 8 bytes -2 63 : 2 63-1 float 4 bytes 2-149 : (2-2 -23 ) 2 127 double 8 bytes 2-1074 : (2-2 -52 ) 2 1023
Strings Built-in non-primitive type String String s1 = my string ; Strings are sequence of characters UF Pugh Hall string literals + means concatenation Pugh + + Hall => Pugh Hall Automatic conversion of numbers to strings Go + 2 + UF => Go2UF Text in a String is immutable
Variables Variables store values myscore = 100; israining = true; monthlysalary = 1656.89; PI = 3.141592653589793; myname = Buzz Aldrin ; All variables must be declared!
Variables Assignment statement = int myscore = 100; boolean israining = true; float monthlysalary = 1656.89; double PI = 3.141592653589793; String myname = Buzz Aldrin ; Variables must store values of the correct type myscore = true; israining = 1; Prevent nonsensical calculations
Initializing Variables Preferred usage: Combine declaration and value initialization in one step: String mycar = Mustang ; Boolean israining = true; Default initial values (unless specified) are based on type.
Default type values Default initial values (unless explicitly specified) are based on type. Type byte 0 short 0 int 0 long Default value 0L float 0.0f double 0.0d char String (or any Object) boolean '\u0000' null false
Methods A set of instructions referenced by a name that can be called whenever required public class Report{ public static void main(string args[]){ printhelloworld(); } } //simple print method public static void printhelloworld(){ System.out.println( Hello World! ); } http://java.sun.com/docs/books/tutorial/java/javaoo/methods.html
Demystifying the main( ) method public static void main (String args[ ]) The keyword public indicates that the method can be called by any object. The keyword static indicates that the method is a class method, which can be called without the requirement to instantiate an object of the class. The keyword void indicates that the method doesn't return any value. The boss function! : When you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application. String args[] - an array of strings : Mechanism through which the runtime system passes command line arguments to your application.
Parameters vs Arguments Parameters specify data that must be provided in an invocation public getname (String ufid, int year) { searchufid = ufid; searchyear=year; } Arguments are the actual values supplied to a method String name = getname ( 1234-5678, 2010);
Naming Conventions Kind Identifier Class GradeBook Variable initialgrade Constant MAXIMUM_GRADE Method getstudentgrade
Java Expressions Compute values of different types Boolean values: 10>5 Numeric values: 2+3, 5%2 String values: Pugh + Hall Object values: new GradeBook();
Control Flow Statements Use a condition to choose which action to take. If, Else If, Else (Multi-way decisions, Nested decisions) Switch-case While, Do-While For http://java.sun.com/docs/books/tutorial /java/nutsandbolts/flow.html
Control Flow Statements If, Else-If, Else (Multi-way decisions, Nested decisions) public String transport() { if (isfrigid()) return "skis"; else if (istropical()) return "surfboard"; else return "bicycle"; } public String dominant() { String animal; if (isfrigid()) { } else if (latitude > 0) animal = "polar bear"; else animal = "penguin"; animal = cat"; } return animal;
Get more info! (Online) Search keywords: Java data types Java basics Floating point numbers Java Class methods vs Instance methods Java statements