an overview of the java programming language
TRANSCRIPT
JAVA
Stuff about Java
• Programming language• Runs on a “virtual machine” (JVM)• Platform Independent (Can run on any
machine)• Object Oriented Language• Automatic Garbage Collection
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”).
Declaring and Assigning Variables
Form: Type name;Examples:• String me = “Kenny”;• int x = 4;• double y;
Operators• Assignment: =• Addition: +• Subtraction: -• Multiplication: *• Division: /Form: varName Op varName;Order of Operation follow standard rules
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;}
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”);}
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: = )
Boolean Operators• &&: logical AND
return true if true • ||: logical OR
returns false if falseExample: if ( x > 6 && x < 9)
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();}
• 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
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!!!
• 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;}
}
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 {
}
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();}
QUESTIONS?