Example of Java Program
Public class Hello
public static void
main(String[]args)
System.out.println(“Hello,World”);
Java and its History
A general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have
as few implementation dependencies as possible. It is intended to let
application developers "write once, run anywhere" (WORA), meaning
that code that runs on one platform does not need to be recompiled to run on another.
Java applications are typically compiled to bytecode (class file)
that can run on any Java virtual machine (JVM) regardless of computer architecture.
Java is, as of 2012, one of the most popular programming languages in
use, particularly for client-server web applications, with a reported 9 million
developers. Java was originally
developed by James Gosling at Sun Microsystems (which has since merged into Oracle Corporation) and released in 1995 as a core
component of Sun Microsystems' Java platform.
The language derives much of its syntax from C and C++,
but it has fewer low-level facilities than either of them.
The original and reference implementation Java compilers, virtual machines, and class libraries were developed by Sun from 1991 and
first released in 1995. As of May 2007, in compliance with the specifications
of the Java Community Process, Sun relicensed most of its Java technologies under the GNU General Public License. Others have also developed alternative implementations
of these Sun technologies, such as the GNU Compiler for Java (bytecode compiler), GNU Class path (standard libraries), and Iced Tea-Web (browser plug in for applets).
Features of Java
JAVA
STATEMENTS
Statements
– specific
one or more actions to be performed during the execution of a program.
Comment – is
an optional statement used to describe what a program or a line of program is
doing such as Block Comments, End-of-line Comments, and JavaDoc Comments.
JAVA
CODING GUIDELINES
Java
program file must have the same as public class and end with java extension.
Use
comments for documentation and readability.
White
space are ignored.
Indent
for readability.
RESERVED
WORDS
A second
category of token is reserved words. Some reserved word includes the following
:
Int, float, double, char, void, public,
static, throw, return.
Reserved
words are also called keywords. The letters in a reserved words are always
lowercase like the special symbol, each reserved word is considered as a single
symbol.
JAVA
LITERALS
Literals
– are
the representation of values.
Integers
Floating point number
Booleans
Strings
Characters
JAVA
IDENTIFIERS
·
Identifiers used to label variables, methods,
classes, etc.
·
Case-sensitive
·
May contain letters, digits, underscore, and
dollar sign.
·
May not start with a digit.
·
May not use java keywords.
JAVA
VARIABLES
Variable – is
an item of data used to store the state of objects composed of data types and
name.
Data Types - indicates
the type of value that the variable can hold.
Name – follows the rule for
identifiers.
DATA
TYPES
– A
set of values together with a set of operators on those values.
PRIMITIVE
DATA TYPES
-
Are the fundamental data types in java.
Categories:
Integral, which
is a data type that deals with
integers or numbers without a decimal part.
Floating point, which
is a data type that deals with logical values.
Boolean, a
data type that deals with logical values.
CLASS
STRING
STRING-
is
a sequence of zero or more characters. String in java is enclosed and in double
quotation or more.
String that contains
no character is called a null or empty string.
Named constant, a
memory location whose content is not allowed to change during program
execution.
The
syntax to declare a named constant is static final data type IDENTIFIER=value;
the reserved words static may or may not appear when a named constant declared.
Java Expressions
Variables
The name of a
variable by itself is an expression. The expression value is the current value
of the variable.
Constants
The name of a
constant by itself is an expression. A literal constant is also an expression.
In either case, the expression value is the value of the constant.
Method Returned
Values
A syntactically
correct method invokation is an expression if the method does not have void return type. To be syntactically correct, a method invokation must
have one argument for each parameter of the method. The arguments must be
expressions separated by commas. The value of each expression must be
convertable to the type declared for the corresponding parameter. The
expression value of a method invokation is the value specified in the return statement of the method.
Operators, Precedence, and
Associativity
Operators are
tokens that are used to combine values in expressions. When evaluating an
expression, Java uses precedence and associativity rules to determine the order
in which operators are applied. These rules mostly are derived from the C
language precedence and associativity rules.In the table below, the Java operators are listed in order of precedence with highest precedence at the top. The rows of the table define precedence groups. Two operators in the same group have the same precedence. Higher precedence operators are applied before lower precedence operators. Except for bitwise operators, the precedence rules give Java expressions the meaning that you would normally expect from experience with mathematical expressions. If an expression behaves contrary to your intention, you can always force a different order of evaluation using parentheses.
Associativity rules are only used to determine the order of evaluation for two operators that are in the same group. Most groups use left-to-right associativity, which means that in an expression with operators in the same precedence group, the operators are applied in left-to-right order. This is what we normally expect for operators that appear in between two operands.
36
In normal mathematical notation, there are only a few cases where we expect associativity to be right-to-left. One is with unary operators that appear before their operand. Java does interpret such expressions according to our expectations, but it is rare that two Java unary operators are used consecutively. The other kind of operators where we normally expect right-to-left associativity is assignments. As usual, Java interprets assignments according to our expectations.
|
Operator group
|
Associativity
|
Type
of operation
|
|
! ~ ++ -- + -
|
right-to-left
|
unary
|
|
* / %
|
left-to-right
|
multiplicative
|
|
+ -
|
left-to-right
|
additive
|
|
<< >> >>>
|
left-to-right
|
bitwise
|
|
< <= > >=
|
left-to-right
|
comparison
|
|
== !=
|
left-to-right
|
comparison
|
|
&
|
left-to-right
|
bitwise
|
|
^
|
left-to-right
|
bitwise
|
|
|
|
left-to-right
|
bitwise
|
|
&&
|
left-to-right
|
boolean
|
|
||
|
left-to-right
|
boolean
|
|
?:
|
right-to-left
|
conditional
|
|
= += -= *= /= %= &=
^= |= <<= >>= >>>= |
right-to-left
|
assignment
|
|
,
|
left-to-right
|
comma
|
|
|
37
|
|
|
|
|
|
Assignment
Expressions
An assignment
expression has the following form. Variable - expression assignment-operator expression
The variable expression can be just the name
of a variable, or it can be an expression that selects a variable using array
indices. The value type of the right-hand-side expression must be compatible
with the variable type.
An assignment
expression is most often used for its side effect: it changes the value of the
variable selected by the variable expression to the value of the expression on
the right-hand side. The value of the assignment expression is the value that
is assigned to the selected variable.In most common assignment expressions, the assignment operator is =. Then the assignment expression has the following form.
Variable - expression = expression
The Java
arithmetic and bitwise operators can be combined with = to form assignment operators. For example, the += assignment operator indicates that the right-hand side should be
added to the variable, and the *= assignment operator indicates that the right-hand side should be
multiplied into the variable. [ error occurred while processing this directive]
Arithmetic Operators
·
The Java programming
language provides operators that perform addition, subtraction, multiplication,
and division. There's a good chance you'll recognize them by their counterparts
in basic mathematics. The only symbol that might look new to you is
"%", which divides one operand by another and returns the remainder
as its result.
+
additive operator (also used for
String concatenation)
-
subtraction operator
*
multiplication operator
/
division operator
%
remainder operator
The following program, ArithmeticDemo, tests the arithmetic operators.
class ArithmeticDemo {
public
static void main (String[] args){
//
result is now 3
int result = 1 + 2;
System.out.println(result);
//
result is now 2
result = result - 1;
System.out.println(result);
//
result is now 4
result = result * 2;
System.out.println(result);
//
result is now 2
result = result / 2;
System.out.println(result);
39
// result
is now 10
result = result + 8;
//
result is now 3
result = result % 7;
System.out.println(result);
}
}
By the end of this
program, the variable thirdString contains "This is a
concatenated string." which gets printed to standard output.
Increment and Decrement
Operators
1.
It is one of the variation of “Arithmetic Operator“.
2.
Increment and Decrement Operators are Unary Operators.
3.
Unary Operator Operates on One Operand.
4.
Increment Operator is Used to Increment Value Stored inside
Variable on which it is operating.
5.
Decrement Operator is used to decrement value of Variable by 1
(default).
Types of Increment and Decrement Operator :
1.
Pre Increment / Pre Decrement Operator
2.
Post Increment / Post Decrement Operator
Syntax :
++ Increment operator : increments a value by 1
-- Decrement operator : decrements a value by 1
Pre-Increment Operator:
1.
“++” is written before Variable name.
2.
Value is Incremented First and then incremented value is used in
expression.
3.
“++” cannot be used over “Constant” of “final Variable“.
40
Assignment Operators
One of the most common
operators that you'll encounter is the simple assignment operator
"=". You saw this operator in the Bicycle class; it assigns the value
on its right to the operand on its left:
int cadence
= 0;
int speed
= 0;
int gear
= 1;
Relational Operator
A relational operator compares two operands to determine whether one is greater than,
greater than or equal to, less than, less than or equal to the other:
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Logical Operators
|
Meaning
|
Note
|
|
|
a && b
|
logical AND
|
short-circuiting
|
|
a || b
|
logical OR
|
short-circuiting
|
|
a & b
|
boolean logical AND
|
not short-circuiting
|
|
a | b
|
41
boolean logical OR
|
not short-circuiting
|
|
a ^ b
|
boolean logical exclusive OR
|
|
|
!a
|
logical NOT
|
|
short-circuiting
|
(x != 0) && (1/x > 1)
|
SAFE
|
|
not short-circuiting
|
(x != 0) & (1/x > 1)
|
NOT SAFE
|
Looping
·
One of the most common programming
structures. It is important since algorithms often need to repeat certain
segments over and over. The three looping statements for java are: for loop,
while loop and do while.
·
Loops are classified into two kinds:
deterministic and non-deterministic.
·
The while and for loop test the termination
condition at the top.
Control Structures in Java
|
||||||||||||||||||||||||||||||||||||||||||||||||
No comments:
Post a Comment