JAVA was developed by James Gosling at Sun Microsystems Inc in the
year 1991, later acquired by Oracle Corporation. It is a simple
programming language. Java makes writing, compiling, and debugging
programming easy. It helps to create reusable code and modular
programs. Java is a class-based, object-oriented programming
language and is designed to have as few implementation dependencies
as possible. A general-purpose programming language made for
developers to write once run anywhere that is compiled Java code can
run on all platforms that support Java. Java applications are
compiled to byte code that can run on any Java Virtual Machine. The
syntax of Java is similar to c/c++.
The Java Class Library (JCL) is a set of dynamically loadable
libraries that Java Virtual Machine (JVM) languages can call at run
time. Because the Java Platform is not dependent on a specific
operating system, applications cannot rely on any of the
platform-native libraries. Instead, the Java Platform provides a
comprehensive set of standard class libraries, containing the
functions common to modern operating systems. JCL serves three
purposes within the JVM:
-
Like other standard code libraries, they provide the programmer a
well-known set of useful facilities, such as container classes and
regular expression processing.
-
The library provides an abstract interface to tasks that would
normally depend heavily on the hardware and operating system, such
as network access and file access.
-
Some underlying platforms may not support all of the features a
Java application expects. In these cases, the library
implementation can either emulate those features or provide a
consistent way to check for the presence of a specific feature.
What you should already know
This guide assumes you have the following basic background:
-
A general understanding of the Internet and the World Wide Web
(WWW).
- Good working knowledge of C and C++.
-
Java is also a platform, which means that Java code can run on any
machine that has a Java Virtual Machine (JVM) on it.
In contrast to Java's compile-time system of classes built by
declarations, JavaScript supports a runtime system based on a small
number of data types representing numeric, Boolean, and string
values. JavaScript has a prototype-based object model instead of the
more common class-based object model. The prototype-based model
provides dynamic inheritance; that is, what is inherited can vary
for individual objects. JavaScript also supports functions without
any special declarative requirements. Functions can be properties of
objects, executing as loosely typed methods.
ava is a general purpose programming language, much like Python or
JavaScript. The language itself is specifically an object oriented
programming language, so bears similarities to C++, C#. Java is also
a platform, which means that Java code can run on any machine that
has a Java Virtual Machine (JVM) on it. Originally those two things
were interchangeable, and the only thing that ran on the JVM was
Java. But since that time, a number of languages have been written
which can run on the Java platform, languages like Scala, Groovy, an
implementation of Ruby called jRuby, and an implementation of Python
called Jython.
Variable in Java is a data container that saves the data values during
Java program execution. Every variable is assigned a data type that
designates the type and quantity of value it can hold. Variable is a
memory location name of the data.
Variable in Java is a data container that saves the data values during
Java program execution. Every variable is assigned a data type that
designates the type and quantity of value it can hold. Variable is a
memory location name of the data. A variable is a name given to a
memory location. For More On Variables please check Variables in Java.
A variable name can consist of Capital letters A-Z, lowercase letters
a-z digits 0-9, and two special characters such as _ underscore and $
dollar sign. The first character must be a letter. Blank spaces cannot
be used in variable names. Java keywords cannot be used as variable
names. Variable names are case-sensitive. The maximum length of the
variable is 64 characters. Variable names always should exist on the
left-hand side of assignment operators.
You can declare a variable in three ways:
With the keyword var. For example, var x = 42.
This
syntax can be used to declare both local and global variables.
By simply assigning it a value. For example,
x = 42.
This always declares a global variable. It
generates a strict JavaScript warning. You shouldn't use this
variant.
With the keyword let. For example, let y = 13.
This
syntax can be used to declare a block scope local variable. See
Variable scope below.
Scope of a variable is the part of the program where the variable is
accessible. Like C/C++, in Java, all identifiers are lexically (or
statically) scoped, i.e.scope of a variable can determined at
compile time and independent of function call stack. Java programs
are organized in the form of classes. Every class is part of some
package. Java scope rules can be covered under following categories.
These variables must be declared inside class (outside any
function). They can be directly accessed anywhere in class.We can
declare class variables anywhere in class, but outside methods.
Access specified of member variables doesn’t affect scope of them
within a class. Member variables can be accessed outside a class
with following rules
if (true) { var x = 5; } System.out(x); // 5
This behavior changes, when using the let declaration introduced in
ECMAScript 2015.
if (true) { let y = 5; } System.out(y); // ReferenceError: y is not
defined
To define a Global variable in java, the keyword static is used.
Java actually doesn’t have the concept of Global variable, it is
known as class variable ( static field ). These are the variables
that can be used by the entire class.
To define the Global Variable, you can just use the static Keyword
like this: public class Example { public static int a; public static
int b; }
You can create a read-only, named constant with the const keyword.
The syntax of a constant identifier is the same as for a variable
identifier: it must start with a letter, underscore or dollar sign
and can contain alphabetic, numeric, or underscore characters.
const PI = 3.14;
A constant cannot change value through assignment or be re-declared
while the script is running. It has to be initialized to a value.
The scope rules for constants are the same as those for let block
scope variables. If the const keyword is omitted, the identifier is
assumed to represent a variable.
You cannot declare a constant with the same name as a function or
variable in the same scope. For example:
// THIS WILL CAUSE AN ERROR function f() {}; const f = 5; // THIS
WILL CAUSE AN ERROR ALSO function f() { const g = 5; var g;
//statements }
However, object attributes are not protected, so the following
statement is executed without problems.
const MY_OBJECT = {"key": "value"}; MY_OBJECT.key =
"otherValue";
The latest ECMAScript standard defines Eight data types:
The Reference Data Types will contain a memory address of variable
values because the reference types won’t store the variable value
directly in memory. They are strings, objects, arrays, etc.
Use the if statement to execute a statement if a logical condition is
true. Use the optional else clause to execute a statement if the
condition is false. An if statement looks as follows:
if (condition) { statement_1; } else { statement_2; }
condition can be any expression that evaluates to true or false. See
Boolean for an explanation of what evaluates to true and false. If
condition evaluates to true, statement_1 is executed; otherwise,
statement_2 is executed. statement_1 and statement_2 can be any
statement, including further nested if statements.
You may also compound the statements using else if to have multiple
conditions tested in sequence, as follows:
if (condition_1) { statement_1; } else if (condition_2) {
statement_2; } else if (condition_n) { statement_n; } else {
statement_last; }
In the case of multiple conditions only the first logical condition
which evaluates to true will be executed. To execute multiple
statements, group them within a block statement ({ ... }) . In
general, it's good practice to always use block statements, especially
when nesting if statements:
if (condition) { statement_1_runs_if_condition_is_true;
statement_2_runs_if_condition_is_true; } else {
statement_3_runs_if_condition_is_false;
statement_4_runs_if_condition_is_false; }
It is advisable to not use simple assignments in a conditional
expression, because the assignment can be confused with equality when
glancing over the code. For example, do not use the following code:
if (x = y) { /* statements here */ }
If you need to use
an assignment in a conditional expression, a common practice is to put
additional parentheses around the assignment. For example:
if ((x = y)) { /* statements here */ }
A while statement executes its statements as long as a specified
condition evaluates to true. A while statement looks as follows:
while (condition) statement
If the condition becomes
false, statement within the loop stops executing and control passes to
the statement following the loop.
The condition test occurs before statement in the loop is executed.
If the condition returns true, statement is executed and the
condition is tested again. If the condition returns false, execution
stops and control is passed to the statement following while.
To execute multiple statements, use a block statement ({ ... }) to
group those statements.
Example:
The following while loop iterates as long as n is less than three:
var n = 0; var x = 0; while (n < 3) { n++; x += n; }
With each iteration, the loop increments n and adds that value to x.
Therefore, x and n take on the following values:
- After the first pass: n = 1 and x = 1
- After the second pass: n = 2 and x = 3
- After the third pass: n = 3 and x = 6
After completing the third pass, the condition n < 3 is no longer
true, so the loop terminates.
A function definition (also called a function declaration, or function
statement) consists of the function keyword, followed by:
- The name of the function.
-
A list of arguments to the function, enclosed in parentheses and
separated by commas.
-
The Java statements that define the function, enclosed in curly
brackets, { }.
For example, the following code defines a simple function named
square:
function square(number) { return number * number; }
The function square takes one argument, called number. The function
consists of one statement that says to return the argument of the
function (that is, number) multiplied by itself. The return
statement specifies the value returned by the function.
return number * number;
Primitive parameters (such as a number) are passed to functions by
value; the value is passed to the function, but if the function
changes the value of the parameter, this change is not reflected
globally or in the calling function.