an overview of the java programming language

16
JAVA

Upload: salaam-kehinde

Post on 10-May-2015

206 views

Category:

Education


3 download

TRANSCRIPT

Page 1: An Overview of the Java Programming Language

JAVA

Page 2: An Overview of the Java Programming Language

Stuff about Java

• Programming language• Runs on a “virtual machine” (JVM)• Platform Independent (Can run on any

machine)• Object Oriented Language• Automatic Garbage Collection

Page 3: An Overview of the Java Programming Language

DATA TYPES

• boolean: Truth value (true or false).• int: Integer (0, 1, -47).• double: Real number (3.14,

1.0, -2.1).• String: Text (“hello world”).

Page 4: An Overview of the Java Programming Language

Declaring and Assigning Variables

Form: Type name;Examples:• String me = “Kenny”;• int x = 4;• double y;

Page 5: An Overview of the Java Programming Language

Operators• Assignment: =• Addition: +• Subtraction: -• Multiplication: *• Division: /Form: varName Op varName;Order of Operation follow standard rules

Page 6: An Overview of the Java Programming Language

MethodsForm: modifiers return_type name(arguments){statements

} To Call:name(arguments);

Examples: public static void myMethod(int arg){int x = 4}public static void myMethod(int arg){int x = 4return x;}

Page 7: An Overview of the Java Programming Language

if and “else if” StatementForm: If (condition){}Example: if(x < 4){

System.out.println(“x is less than 4”);} else if(x > 4){System.out.println(“x is greater than 4”);} else{System.out.println(“x must be equal to 4”);}

Page 8: An Overview of the Java Programming Language

COMPARISON OPERATORS

• x > y: x is greater than y • x < y: x is less than y • x >= y: x is greater than or equal to x • x <= y: x is less than or equal to y• x == y: x equals y • ( equality: ==, assignment: = )

Page 9: An Overview of the Java Programming Language

Boolean Operators• &&: logical AND

return true if true • ||: logical OR

returns false if falseExample: if ( x > 6 && x < 9)

Page 10: An Overview of the Java Programming Language

Loops

Loops Informationwhile Runs while condition is true

While(x < 4){dostuff();}

do … while Runs for the first time without checking then runs while condition is truedo{Stuff();}while(x < 4);

For Runs with optional counter while condition is trueFor(int I = 0; I < 4, x < 4; i++){doStuff();}

Page 11: An Overview of the Java Programming Language

• An indexed list of values• Any data type• All elements must have the same type• Form: type[] name = { val1, val2, val3 }; OR

type[ ] name = new type[noOfValues];

Int [] a = { 3, 5, 6, 8, 11, 12, 15, 17, 19, 20 }

Value:

Index: a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]

System.out.println(a[5]); output: 12

Arrays

Page 12: An Overview of the Java Programming Language

ClassesTo model real world problems

// little baby 1String name1;double weight1;// little baby2 String name2;double weight2;// little baby 3String name3;double weight3;

For 500 babies inefficient!!!

Page 13: An Overview of the Java Programming Language

• Using Classes Create a class Baby Then an array of baby or babies

public class Baby {String name; boolean isMale; double weight; double decibels;

}

Baby[] babies = new Baby[500];

• Classes can also contain methodspublic class Baby {

…Eat(){

weight += 1;}

}

Page 14: An Overview of the Java Programming Language

Inheritance

• When a class inherits another class it has the characteristics of that class plus its own.

• Classes inherit from one class• Example: A class Car can inherit from another

class vehicle.• Form: modifier class className extends baseClass {

}

Page 15: An Overview of the Java Programming Language

Interfaces

• Interfaces are like classes that cannot have object instances assigned.

• They can only be implemented by classes.• Classes can implement many interfaces.• Example:

interface move {void accelerate();void speed();}

Page 16: An Overview of the Java Programming Language

QUESTIONS?