Thursday, 3 March 2016

Basics of Java

Basics of Java

class
A class can be defined as a blue print that describes the behaviors/states of the object, for which individual objects are created. 
A sample of a class is given below:

  • Local Variables are variables defined inside methods, constructors or blocks.
  • Instance variables are variables within a class but outside any method.
  • Class variables are variables declared with in a class, outside any method, with the static keyword.
Objects
  • If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging, running
  • If you compare the software object with a real world object, they have very similar characteristics.
  • Software objects also have a state and behavior. A software object's state is stored in fields and behavior is shown via methods.
Constructors
  • Every class has a constructor. If we do not explicitly write a constructor for a class the Java compiler builds a default constructor for that class.
  • Each time a new object is created, at least one constructor will be invoked. 
  • The main rule of constructors is that they should have the same name as the class. 
  • A class can have more than one constructor.

How to Run a program
Step1: Open a note pad and save the document with .java extension.

Step2: Now start writing the program and the class and the file name should be same. Save the program.

Step3: Open the command prompt and set the path of the folder in which it has been saved.

Looping
loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:
Java Tutorial
Java programming language provides the following types of loop to handle looping requirements.
Loop Type Description
while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
do...while loop Like a while statement, except that it tests the condition at the end of the loop body.
Syntax of "for" loop
          for(declaration : expression)
{
//statements
}

Loop control statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Control Statement Description
break statement Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
continue statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

Decision making
Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed.
Java Tutorial
Statement Description
if statement An if statement consists of a boolean expression followed by one or more statements.
if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
nested if statements You can use one if or else if statement inside another if or else if statement(s).
switch statement A switch statement allows a variable to be tested for equality against a list of values.

No comments:

Post a Comment

: