Java basics
• Java is a
programming language originally developed by James Gosling at Sun Microsystems.
Java applications are typically compiled to byte code (class file) that can run
on any Java Virtual Machine (JVM) regardless of computer architecture.
• It is
intended to let application developers "write once, run anywhere".
Java is general-purpose, concurrent, class-based, and object-oriented language.
Variables
• Local Variables
Similar to how an object stores its state in
fields, a method will often store its temporary state in local variables. The
syntax for declaring a local variable is similar to declaring a field (for
example, int count = 0;).
‖ There is no special keyword designating a
variable as local; that determination comes entirely from the location in which
the variable is declared — which is between the opening and closing braces of a
method.
Local variables are only visible to the methods
in which they are declared; they are not accessible from the rest of the class.
• Instance Variables (Non-Static Fields)
Objects store their individual states in
"non-static fields", that is, fields declared without the static
keyword.
Non- static fields are also known as instance
variables because their values are unique to each instance of a class (to each
object, in other words
Class Variables (Static Fields)
A class variable is any field declared with the
static modifier; this tells the compiler that there is exactly one copy of this
variable in existence, regardless of how many times the class has been
instantiated.
Operators
• Operators
are special symbols that perform specific operations on one, two, or three
operands, and then return a result.
• The
closer to the top of the table an operator appears, the higher its precedence.
Operators with higher precedence are evaluated before operators with relatively
lower precedence. Operators on the same line have equal precedence. When
operators of equal precedence appear in the same expression, a rule must govern
which is evaluated first.
• All
binary operators except for the assignment operators are evaluated from left to
right; assignment operators are evaluated right to left.
Operator Precedence
Operators Precedence
postfix expr++
expr--
unary ++expr
--expr +expr -expr ~ !
multiplicative *
/ %
additive +
-
shift <<
>> >>>
relational <
> <= >= instanceof
equality ==
!=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ?
:
assignment =
+= -= *= /= %= &= ^= |= <<= >>= >>>=
• The
assignment operator "=" is far more common than the unsigned right
shift operator ">>>". The signed left shift operator
"<<" shifts a bit pattern to the left and the signed right
shift operator ">>" shifts a bit pattern to the right.
• The bit
pattern is given by the left-hand operand and the number of positions to shift
by the right-hand operand. The unsigned right shift operator
">>>" shifts a zero into the leftmost position, while the
leftmost position after ">>" depends on sign extension.
• The
instanceof operator compares an object to a specified type. You can use it to
test if an object is an instance of a class, an instance of a subclass, or an
instance of a class that implements a particular interface.
• The
following program, InstanceofDemo, defines a parent class (named Parent), a
simple interface (named MyInterface), and a child class (named Child) that
inherits from the parent and implements the interface.
class
InstanceofDemo {
public static void main(String[] args) { Parent
obj1 = new Parent(); Parent obj2 = new Child();
System.out.println("obj1
instanceof Parent: " + (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: " + (obj1 instanceof
Child)); System.out.println("obj1 instanceof MyInterface: "+(obj1
instanceof MyInterface)); System.out.println("obj2 instanceof Parent:
" + (obj2 instanceof Parent)); System.out.println("obj2 instanceof
Child: " + (obj2 instanceof Child)); System.out.println("obj2
instanceof MyInterface: "+(obj2 instanceof MyInterface));
}
}
class
Parent{}
class
Child extends Parent implements MyInterface{} interface MyInterface{}
Output:
obj1
instanceof Parent: true obj1 instanceof Child: false
obj1
instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof
Child: true
obj2
instanceof MyInterface: true
When
using the instanceof operator, keep in mind that null is not an instance of
anything.
Control Flow Statements
‖ The
statements inside source files are generally executed from top to bottom, in
the order that they appear.
Control
flow statements, however, break up the flow of execution by employing decision
making, looping, and branching, enabling your program to conditionally execute
particular blocks of code.
• The
Control Flow Statements are decision-making statements (if-then, if-then-else,
switch), the looping statements (for, while, do-while), and the branching
statements (break, continue, return) supported by the Java programming
language.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.