Java Elements

Looping Constructs in Java - While, Do While, Break and Continue

The while statement

The “while loop” is an alternative to “for loop”. a “for loop” is usually used when the number of times the block is to be executed is known. A “while loop” is usually used when the number of times the block is to be executed is not known, like prompting the user whether to stop executing during every iteration.

The syntax of “while loop” is:

while (<condition>) <statements>;

Example:

Access Modifiers in Java

Access modifiers are used to specify the accessibility or access levels of a type (class, interface) and its members (methods, variables and even constructors). There are three access modifiers and four access levels in Java. The three access modifiers are are private, protected and public. Four access levels (from most restricted to least restricted) are private, default (no modifier), protected and public. 

Constructors in Java

Constructors are used to initialize the state of an object when it is created. Constructors are invoked while creating objects, usually after the new keyword. A child class may also invoke a super constructor using the super keyword to initialize the parent object.

Constructors are similar in syntax to methods, but without any return type and have the same name as that of the class (case sensitive).

MyClass{

  MyClass(){

  }

Keyword this in Java

The “this keyword” in Java refer to the the current object (current instance). All your instance variables may be referred as 'this.variableName' even though it is implicit in most cases. 

The “this keyword”  is mostly useful in below cases:

  1. Refer to instance variables of a class when a local variable has precedence over it.

Classes and Objects in Java

Java is a programming language that follows the Object-Oriented Programming (OOP) principles. OOP model focusses on realworld objects to model applications. A real world object will have some properties and some behaviour. For example, human will have properties like height, weight etc. and behaviours like walk, run talk, etc.  

 

Looping Constructs in Java - For and For-Each

Looping constructs in Java are:

  • For statement

  • For-each statement

  • While statement

  • Do while statement

We will see for and for-each here. We will see while, do while, break and continue statements in next note. 

 

The for statement

Syntax of for loop is:

Traversing, Comparing and Copying Arrays

Traversing arrays

Traversing an array is the process of visiting each element of an array.

Two common ways to traverse an array are:

  • simple for loop / while loop

  • for-each loop

 

Example array

int[] myArray = {1,2,3,4,5}

 

Decision Control Constructs in Java

Decision control statements can change the execution flow of a program. 

Decision control statements in Java are:

  • if statement

  • Conditional operator

  • switch statement

 

Literals in Java

 

Literals are numbers, characters, and string representations in your program. Literals can be classified as numeric literals, character literals and string literals.

 

Introduction to Multithreading in Java

In this article, we start by discussing the difference between multithreading and multitasking (or difference between threads and processes), then discussing the thread states, different ways of creating threads and a simple thread example to make things clear. We will also learn what are deamon threads.

Pages

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!