Hellomessage Variable Is Declared As A Local Variable:: Public Class Public Static Void
Hellomessage Variable Is Declared As A Local Variable:: Public Class Public Static Void
public class MainClass { public static void main(String[] args) { String helloMessage; helloMessage = "Hello, World!"; System.out.println(helloMessage); } }
y y y y y y
Hypotenuse is 5.0
true
b is b is This 10 >
Boolean Variables
1. Variables of type boolean can have only one of two values, true or false. 2. The values 'true' and 'false' are boolean literals. 3.
public class MainClass{
public static void main(String[] arg){ boolean state = true; state = false; System.out.println(state); } }
false
a = 6 b = 8 c = 3
is a String
However, applying instanceof on a null reference variable returns false. For example, the following if st returns false.
public class MainClass { public static void main(String[] a) { String s = null; if (s instanceof java.lang.String) { System.out.println("true"); } else { System.out.println("false"); } } }
false
Since a subclass 'is a' type of its superclass, the following if statement, where Child is a subclass of Pa returns true.
class Parent { public Parent() { } } class Child extends Parent { public Child() { super(); } } public class MainClass { public static void main(String[] a) { Child child = new Child(); if (child instanceof Parent) { System.out.println("true"); } } }
true
Identifying Objects
class Animal { public String toString() { return "This is an animal "; } } class Dog extends Animal { public void sound() { System.out.println("Woof Woof"); } } public class MainClass { public static void main(String[] a) { Dog aDog = new Dog(); if (aDog instanceof Animal) { Animal ani = (Animal) aDog; System.out.println(ani); } } }
This is an animal
} class B { int i, j; } class C extends A { int k; } class D extends A { int k; } class InstanceOf { public static void main(String args[]) { A a = new A(); B b = new B(); C c = new C(); D d = new D(); if (a instanceof A) System.out.println("a if (b instanceof B) System.out.println("b if (c instanceof C) System.out.println("c if (c instanceof A) System.out.println("c is instance of A"); is instance of B"); is instance of C"); can be cast to A");
if (a instanceof C) System.out.println("a can be cast to C"); System.out.println(); A ob; ob = d; // A reference to d System.out.println("ob now refers to d"); if (ob instanceof D) System.out.println("ob is instance of D"); System.out.println(); ob = c; // A reference to c System.out.println("ob now refers to c"); if (ob instanceof D) System.out.println("ob can be cast to D"); else System.out.println("ob cannot be cast to D"); if (ob instanceof A) System.out.println("ob can be cast to A"); System.out.println();
// all objects can be cast to Object if (a instanceof Object) System.out.println("a may be cast to if (b instanceof Object) System.out.println("b may be cast to if (c instanceof Object) System.out.println("c may be cast to if (d instanceof Object) System.out.println("d may be cast to } }
Classes are the fundamental building blocks of a Java program. You can define an Employee class as
1. 2. 3. 4.
By convention, class names capitalize the initial of each word. For example: Employee, Boss, DateUtility, PostOffice, RegularRateCalculator. This type of naming convention is known as Pascal naming convention. The other convention, the camel naming convention, capitalize the initial of each word, except th word. 5. Method and field names use the camel naming convention.
Fields 1. Fields are variables. 2. They can be primitives or references to objects. For example, the Employee class has two fields, age and salary.
public class Employee{ int age; int salary }
1. Field names should follow the camel naming convention. 2. The initial of each word in the field, except for the first word, is written with a capital letter. 3. For example: age, maxAge, address, validAddress, numberOfRows. Defining Classes: A class has fields and methods
public class MainClass { private int aField; public void aMethod() { } }
it is an animal!
Class that contains a String instance variable and methods to set an its value
public class MainClass { public static void main( String args[] ) { GradeBook myGradeBook = new GradeBook(); System.out.printf( "Initial course name is: %s\n\n",myGradeBook.getCourseName() ); String theName = "Java"; myGradeBook.setCourseName( theName ); // set the course name myGradeBook.displayMessage(); } } class GradeBook {
private String courseName; // course name for this GradeBook // method to set the course name public void setCourseName( String name ) { courseName = name; // store the course name } // method to retrieve the course name public String getCourseName() { return courseName; } // display a welcome message to the GradeBook user public void displayMessage() { System.out.printf( "Welcome to the grade book for\n%s!\n", getCourseName() ); } }
Initial course name is: null Welcome to the grade book for Java!
System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() ); System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() ); } } class Account { private double balance; // instance variable that stores the balance // constructor public Account( double initialBalance ) { if ( initialBalance > 0.0 ) balance = initialBalance; } public void credit( double amount ) { balance = balance + amount; } public double getBalance() { return balance; } }
account1 balance: $50.00 account2 balance: $0.00 account1 balance: $60.10 account2 balance: $0.00 account1 balance: $60.10 account2 balance: $12.12
class B { static A a = new A(1); B() { System.out.println("Table()"); staticA.f(1); } void f2(int marker) { System.out.println("f2(" + marker + ")"); } static A staticA = new A(2); } class C { A a = new A(3); static A staticA = new A(4); C() { System.out.println("Cupboard()"); staticA.f(2); } void f3(int marker) { System.out.println("f3(" + marker + ")"); } static A staticA2 = new A(5); } public class MainClass { public static void main(String[] args) { System.out.println("Creating new Cupboard() in main"); new C(); System.out.println("Creating new Cupboard() in main"); new C(); t2.f2(1); t3.f3(1); } static B t2 = new B(); static C t3 = new C(); }
Cupboard() f(2) Creating new Cupboard() in main Bowl(3) Cupboard() f(2) Creating new Cupboard() in main Bowl(3) Cupboard() f(2) f2(1) f3(1)
Using Constructors
1. 2. 3. 4. 5. 6. 7.
Every class must have at least one constructor. If there is no constructors for your class, the compiler will supply a default constructor(no-arg c A constructor is used to construct an object. A constructor looks like a method and is sometimes called a constructor method. A constructor never returns a value A constructor always has the same name as the class. A constructor may have zero argument, in which case it is called a no-argument (or no-arg, for constructor. 8. Constructor arguments can be used to initialize the fields in the object. The syntax for a constructor is as follows.
constructorName (listOfArguments) { [constructor body] } public class MainClass { double radius; // Class constructor MainClass(double theRadius) { radius = theRadius; } }
Multiple Constructors
class Sphere { int radius = 0; Sphere() { radius = 1; } Sphere(int radius) { this.radius = radius; } }
double d = 8.95; float f = 2.1f; int i = 63; long l = 2L; short sh = 200; String str = "test";
public static void main(String[] args) { System.out.println("bool = " + bool); System.out.println("by = " + by); System.out.println("ch = " + ch); System.out.println("d = " + d); System.out.println("f = " + f); System.out.println("i = " + i); System.out.println("l = " + l); System.out.println("sh = " + sh); System.out.println("str = " + str); } }
PortableLunch() { System.out.println("PortableLunch()"); } } class Sandwich extends PortableLunch { private Bread b = new Bread(); private Cheese c = new Cheese(); private Lettuce l = new Lettuce(); public Sandwich() { System.out.println("Sandwich()"); } } public class MainClass { public static void main(String[] args) { new Sandwich(); } }