Core Java

Java Evolution
Java History
Java Features
Benefits of Java over C and C++
How Java works
The Java Programming Language
The Java Platform
Java Development Kit
HARDWARE AND SOFTWARE REQUIREMENTS
Disadvantages of Java
Overview of Java Language
Developing the Java Application
Comments in Java Code
Defining a Class
The main Method
Using Classes and Objects
Java Program Structure
Java Tokens
Constant, Variable and Datatype
Variables
Declaring Variables
Rules on Variable Names
Variable Types
Primitive Data Types
Constants
Operator and Expression
Expressions and Operators
Operator Precedence
Integer Operators
Relational Integer Operators
Floating point Operators
Relational Floating-Point Operators
String Operator
Assignment Operators
Arithmetic assignment operations
Conversions
Casts
Boolean expressions and operations
Logical operators
Bitwise operations
The complement operator
Decision making, Branching and Looping
Flow control with if and else
The if statement
The else statement
Switch statements
for, while, and do-while statements
The for statement
The while statement
The do-while statement
Using break and continue
The break statement
The continue statement
Class Object and Method
Introduction to Classes
The Benefit of Classes
Defining Classes
Class using constructor
Object
Declaring an Object
Instantiating an Object
Initializing an Object
Referencing an Object's Variables
Calling an Object's Methods
Creating a Class
The Class Declaration
The Class Body
Constructors for Classes
Implementing Methods
The Method Body
A Method's Name
Example of the Math class
The Applet Package
Array and String
Arrays
Java arrays
Object arrays
Strings
String Concatenation
Inheritance
Introduction
Creating Subclasses
Member Variables In Subclass Inherit?
Hiding Member Variables
Methods In Subclass Inherit?
Overriding Methods
Methods a Subclass Cannot Override
Methods a Subclass Must Override
The Benefits of Inheritance
Interfaces
Interfaces and Classes
Implementing and Using Interfaces
Implementing Multiple Interfaces
Creating and Extending Interfaces
Methods Inside Interfaces
Extending Interfaces
Package
Introduction
Declaring Packages
Importing Packages
Creating Our Own Packages
The Java Language Package
The Java I/O Package
The Java Utility Package
The Java Networking Package
The Applet Package
The Abstract Window Toolkit Packages
Multithreading
Introduction
Thread
Thread Attributes
Thread State
Thread Group
Methods that Operate on the Group
Access Restriction Methods
The notifyAll() and wait() Methods
Frequently used Method
Exception Handling
Introduction
What Is an Exception?
If Exceptions than?
The exception handling technique
Some Terminology
Throw an Exception
Throw, try, and catch Blocks
Multiple catch Blocks
The finally Clause
The Throwable Class
Types of Exceptions
Different List of Exception
Built-In Exceptions
Applet
Introduction
How Applets and Applications Are Different
Limitation of Applet
The Applet class
Major Applet Activities
The life cycle of a Web page applet
Including an Applet on a Web Page
Essential HTML to launch an applet and pass it parameters
Launching an applet in an HTML document
A sample applet that receives a parameter
Posting a Web page that launches a custom applet
Managing Input/Output Files in Java
Introduction
Streams
Input Streams
The Abstract Class InputStream
The File class
The FileDialog class
Low-level and high-level stream classes
The FileOutputStream class
The FileInputStream class
The DataOutputStream class
The DataInputStream class
The ObjectOutputStream class
The ObjectInputStream class

Decision making, Branching and Looping


Switch statements

Flow control with if and else statements gets cumbersome when a variable must be tested for a large number of possible values, such as a menu selection that permits the user to enter an integer from 1 to 20. The solution to this problem is the switch statement.
 
The switch statement
 
1) Specifies an expression whose value is be tested. This is known as the "switch      expression" and is typically the name of a single variable.
 
2) Defines a number of "cases", each associated with a different value. There may also be     a "default" case that is not associated with a value. If the value of the switch expression     is equal to the value of a case, the statements for that case will be executed. Otherwise,     the statements of the default case will be executed.
 
3) General syntax:
 
switch (expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
break;
}
 
It has a number subtleties and restrictions:
 
1. The switch expression must be a primitive of type int or able to be promoted to int.     Specifically, it must be either byte, short, int, or char. Expressions of type boolean, long,     float, and double will not compile.
 
2. The value specified on a case must be a constant of type int or must be able to be     promoted to int (in other words a byte, short, int, or char). It must also be within the     possible range of values of the switch expression. For example, if the switch variable is a     byte, a case with a value of 200 would not compile because a byte may only have a     value from -128 to 127.
 
The value of a case may be an expression as long as the result is a constant.
 
For example,
case 5 + 1:
would compile successfully.
 
3. The break statement is optional and will be covered in more detail in a later lesson.     When encountered, it ends the execution of the switch statement. If omitted from a     case, processing falls through to the next case (a sometimes undesirable result).
 
4. Although the compiler doesn't care, cases should be arranged in a high probability to low     probability order to enhance processing efficiency. The default can be placed anywhere     (even first).
 
It may be nested.
A switch can be coded within another switch or in either leg of an if-else.
It can also contain if-else code.
 
Example:
 
import java.util.*;
import java.io.*;
import java.lang.*;
public class Aman
{
public static void main(String[] args)
{

// Variables
// Prompt for and read data

System.out.print("Enter customer's starting balance: ");
Scanner Keyboard=new Scanner(System.in);
Double balance;
balance = Keyboard.readDouble();
System.out.print("Enter transaction amount: ");
double amount;
amount = Keyboard.readDouble();
System.out.println("Transaction codes are");
System.out.println(" " + "C - charge");
System.out.println(" " + "P - payment");
System.out.println(" " + "R - refund or return");
System.out.print("Enter transaction code: ");
char code;
code = Keyboard.readChar();

// Process based upon transaction code

switch (code)
{
case 'C':
case 'c':
balance += amount;
System.out.println("New balance is " + Utility.moneyFormat(balance));
break;
case 'P':
case 'p':
balance -= amount;
System.out.println("New balance is " + Utility.moneyFormat(balance));
break;
case 'R':
case 'r':
balance -= amount;
System.out.println("New balance is " + Utility.moneyFormat(balance));
break;
default:
System.out.println("Invalid transaction code");
break;
}
}
}
***try it with the help of under given suggestion.
 
Notes:
 
1. The balance, code, and amount variables hold data entered by the user.
 
2. After all data has been read from the user, the transaction is processed by the switch     statement with the transaction code (code) used as the switch expression.
 
3. Within the switch, if the value of code matches the value of a particular case, processing     jumps to the statement block for that case, otherwise processing jumps to the statement     block for the default. Processing will then continue until either a break statement is     encountered or the end of the switch is reached.
 
4. Stacking two or more cases without at break statement constitutes an OR.
 
For example,
case 'R':
case 'r':
 
will result in the same processing being performed if the value of code is either an uppercase or lowercase 'R'.
 
5. For transactions with a valid transaction code, the customer's new balance is displayed     using the moneyFormat() method of my Utility class. If the transaction code is bad, an     error message is displayed.