Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sridhar Narayan narayans@uncw.edu Java Basics Sridhar Narayan narayans@uncw.edu.

Similar presentations


Presentation on theme: "Sridhar Narayan narayans@uncw.edu Java Basics Sridhar Narayan narayans@uncw.edu."— Presentation transcript:

1 Sridhar Narayan narayans@uncw.edu
Java Basics Sridhar Narayan

2 Java programs A Java program is a collection of classes
Each class is a generic description (template) of an object being modeled Each class describes Properties of the object being modeled, i.e. structure of the object Ways in which one can interact with the object, i.e. behavior of the object. These are called methods. The main method in a class is where execution begins when that class is run

3 Java methods Each method is a collection of statements
Each statement is terminated by a semi-colon Java is case sensitive Each variable must be declared before use int, float, double, char, boolean are primitive types Every Java class is a non-primitive type Java employs block scoping Each pair of {} defines a block Variables declared within a block are visible only within that block

4 Java comments // single line comment /* multi-line comment */
/** used for Java docs, similar to Python docstring comments **/

5 Java types Unlike Python, Java does not infer types. Types are explicitly declared int x = 10; boolean p = false; Java is strongly typed. float x = 2.5; //error, since 2.5 is a double float x = 2.5f; // ok Statically type checked All type errors are caught during compilation

6 Compiled and then interpreted
The java compiler first compiles Java source code (.java files) into Java byte code (.class files) The byte code is then interpreted by the Java VM (virtual machine) In theory, once compiled, Java code can be run on any platform for which a JVM exists.

7 Iterative constructs for(int i=0; i < 10; i++) { //do something }
while (condition is true) { //do something } do { //something } while (condition is true);

8 Selection if- else if - else Use parenthesis for conditions:
if (a > b) { //do something } else if (c > d) { //do something else } else { //do a third thing } && (and), || (or) if (a > b && c < d || a == b) { //do something }

9 Arrays int x; //declare x as an integer. x is a primitive
int [] data; // declare data as a reference to an array of integers. No array exists at this point data = new int[100]; //instantiate and assign an array of 100 integers to data Declaration and instantiation can be combined int [] data = new int[100]; Array index values start with 0. For example above, valid references are data[0]…data[99]


Download ppt "Sridhar Narayan narayans@uncw.edu Java Basics Sridhar Narayan narayans@uncw.edu."

Similar presentations


Ads by Google