Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides.

Similar presentations


Presentation on theme: "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides."— Presentation transcript:

1 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides 17 May 2010

2 JAVA Duke, the JAVA mascot Java 1.0 was first released by Sun Microsystems in 1995. Java became popular for web programming. "Write Once, Run Anywhere" (WORA) philosophy. Key design principles: -Platform independence -Security -Stability

3 How Java works!

4 Programming in JAVA – I Basic concepts in Java Values Variables Complex data/objects Methods Expressions Conditionals Loops

5 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

6 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

7 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

8 Programming in JAVA - II Numeric Types in Java are characterized by their size (how much memory they occupy) Integral types Floating point types SizeRange (inclusive) byte1 byte-128: 127 short2 bytes-32,768: 32,767 char2 bytes0: 65,535 int4 bytes-2 31 : 2 31 -1 long8 bytes-2 63 : 2 63 -1 float4 bytes2 -149 : (2-2 -23 )·2 127 double8 bytes2 -1074 : (2-2 -52 )·2 1023

9 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

10 Variables Variables store values myScore = 100; isRaining = true; monthlySalary = 1656.89; PI = 3.141592653589793; myName = “Buzz Aldrin”; All variables must be declared!

11 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

12 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.

13 Default type values Default initial values (unless explicitly specified) are based on type. TypeDefault value byte0 short0 int0 long0L float0.0f double0.0d char'\u0000' String (or any Object) null booleanfalse

14 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

15 Demystifying the main( ) method public static void main (String args[ ]) “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 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. String args[] - “an array of strings”: Mechanism through which the runtime system passes command line arguments to your application.

16 “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);

17 Naming Conventions KindIdentifier ClassGradeBook VariableinitialGrade ConstantMAXIMUM_GRADE MethodgetStudentGrade

18 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();

19 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 http://java.sun.com/docs/books/tutorial /java/nutsandbolts/flow.html

20 { if (latitude > 0) animal = "polar bear"; else animal = "penguin"; } { if (latitude > 0) animal = "polar bear"; else animal = "penguin"; } 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"; }

21 Get more info! (Online) Search keywords: Java data types Java basics Floating point numbers Java Class methods vs Instance methods Java statements


Download ppt "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Programming in JAVA – II (A Whirlwind Tour of Java) Course Lecture Slides."

Similar presentations


Ads by Google