210 likes | 361 Views
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. Java became popular for web programming.
E N D
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. Java became popular for web programming. Duke, the JAVA mascot "Write Once, Run Anywhere" (WORA) philosophy. • Key design principles: • Platform independence • Security • Stability
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: classinstance • Variable belonging to an object: instancevariable
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
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.
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 staticvoidmain(String args[ ]) The keyword public indicates that the method can be called by any object. “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. 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. 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);
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(); http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
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 dominant() { String animal; if (isFrigid()) else animal = “cat"; return animal; } public String transport() { … if (isFrigid()) return "skis"; else if (isTropical()) return "surfboard"; else return "bicycle"; } { if (latitude > 0) animal = "polar bear"; else animal = "penguin"; }
Get more info! (Online) Search keywords: • Java data types • Java basics • Floating point numbers • Java Class methods vs Instance methods • Java statements