Java Basics Study Guide: We Lcome
Java Basics Study Guide: We Lcome
Java Basics Study Guide: We Lcome
NEWSLETTER
• Basic terms
• Program
• Programming Language
High-level, Low-level and
• Machine language
• Editor, Compiler, Interpreter,
JAM ES GOSLING •
Debugger, IDE
The Java Platform
• Language characteristics
• Evolution
Java execution environment (from
source code to executable)
Although many languages share similarities, each has its own syntax. Once a
programmer learns the languages rules, syntax, and structure, they write the source
code in a text editor or IDE. Then, the programmer often compiles the code
into machine language that can be understood by the computer. Scripting languages,
which do not require a compiler, use an interpreter to execute the script.
ActionScript D LeLisp R
ALGOL DarkBASIC Lisp Racket
Ada Dart LiveScript Reia
AIML * Datalog LOGO RPG
Altair BASIC dBASE Lua Ruby
Assembly Dylan MACLISP Rust
AutoHotkey EuLisp Matlab Scala
Babel Elixir Metro Scheme
BASIC F MUMPS Scratch
Batch file F# Nim SGML *
BCPL FORTRAN Objective-C Simula
BeanShell FoxPro OCaml Smalltalk
Brooks Franz Lisp Pascal SPL
C Go Perl SQL *
C# GW Basic PHP Stanford LISP
C++ Haskell Pick Swift
CL HDML * PureBasic Tcl
Clojure HTML * Python Turbo Pascal
COBOL InterLisp Prolog True BASIC
CoffeeScript ksh QBasic VHDL
Common Lisp Java Visual Basic
CPL JavaScript Visual FoxPro
CSS * JCL WML *
Curl Julia WHTML *
Curry Kotlin XLISP
XML *
YAML *
ZetaLisp
What are the top programming
languages?
There are many different ways this question could be
answered. However, we feel that the best method is to rely on the
data from GitHub, a service that hosts over 96 million different
software projects. In the below chart, GitHub shows the top 10
programming languages from 2014 to 2018.
Advantages
Low level languages are used to write programs that relate to the specific architecture and hardware of
a particular type of computer. They are closer to the native language of a computer (binary), making them harder for
programmers to understand.
Examples of low level language:
Assembly Language
Machine Code
Assembly Language
Few programmers write programs in low level assembly language, but it is still used for developing code for specialist
hardware, such as device dvers.
It is easy distinguishable from a high level language as it contains few recognisable human words but plenty
of mnemonic code.
Advantages
Can make use of special hardware or special machine-dependent instructions (e.g. on the specific chip)
Translated program requires less memory
Write code that can be executed faster
Total control over the code
Can work directly on memory locations
Our Translators page explains how low-level languages are translated into machine code.
Machine Code
Programmers rarely write in machine code (binary) as it is difficult to understand.
The term EDITOR is commonly used to refer to a text editor, which is a software program that allows
users to create or manipulate plain text computer files. They are often used in the field of computer
programming.
Emacs - Text editor for all platforms that is a very powerful text editor
Vi and Vim - Other great editors primarily used with Linux but also
EDITORS
COMPILER
A compiler is a special program that processes statements written in a particular programming language
and turns them into machine language or "code" that a computer's processor uses. Typically, a programmer writes
language statements in a language such as Pascal or C one line at a time using an editor. The file that is created
contains what are called the source statements. The programmer then runs the appropriate language compiler,
specifying the name of the file that contains the source statements.
When executing (running), the compiler first parses (or analyzes) all of the language statements
syntactically one after the other and then, in one or more successive stages or "passes", builds the output code,
making sure that statements that refer to other statements are referred to correctly in the final code. Traditionally,
the output of the compilation has been called object code or sometimes an object module. (Note that the term
"object" here is not related to object-oriented programming.) The object code is machine code that the processor
can execute one instruction at a time.
Examples of Compiler
C/C++ Microsoft Visual C++ (cl.exe)
Borland C (bcc.exe, bcc32.exe)
GNU Compiler (linux: gcc/g++, windows: mingw-
gcc.exe, mingw-g++.exe)
Open Watcom (wcc.exe, wcc32.exe)
Even the
most experienced
software
programmers
usually don't get it
right on their first
try. Certain errors,
often called bugs,
can occur in
programs, causing
them to not
Fortunately, there are programs called debuggers that help software developers find and eliminate bugs while
they are writing programs. A debugger tells the programmer what types of errors it finds and often marks the
exact lines of code where the bugs are found. Debuggers also allow programmers to run a program step by step
so that they can determine exactly when and why a program crash. Advanced debuggers provide detailed
information about threads and memory being used by the program during each step of execution.
A debugger is a computer program used by programmers to test and debug a target program. Debuggers may
use instruction-set simulators, rather than running a program directly on the processor to achieve a higher level
of control over its execution. This allows debuggers to stop or halt the program according to specific conditions.
However, use of simulators decreases execution speed.
When a program crashes, debuggers show the position of the error in the target program. Most debuggers also
are capable of running programs in a step-by-step mode, besides stopping on specific points. They also can
often modify the state of programs while they are running.
Integrated Development Environment (IDE)
An Integrated Development Environment (IDE) is an application that facilitates application development. In
general, an IDE is a graphical user interface (GUI)-based workbench designed to aid a developer in building
software applications with an integrated environment combined with all the required tools at hand.
Most common features, such as debugging, version control and data structure browsing, help a developer
quickly execute actions without switching to other applications. Thus, it helps maximize productivity by providing
similar user interfaces (UI) for related components and reduces the time taken to learn the language. An IDE
supports single or multiple languages.
TOP 10 best IDE software programs.
Microsoft Visual Studio. ...
NetBeans. ...
PyCharm. ...
IntelliJ IDEA. ...
Eclipse. ...
Code::Blocks. ...
Aptana Studio 3. ...
Komodo.
RubyMine
Xcode
EDITOR INTERPRETER
DEBUGGER
COMPILER
IDE
LANGUAGE
1) Java is SIMPLE CHARACTERISTICS
The Java programming language is easy to learn and use. Java code is easy to read and write.
2) Java is SECURE
The Java platform is designed with security features built into the language and runtime system such as static
type-checking at compile time and runtime checking (security manager), which let you creating applications that
can’t be invaded from outside. You never hear about viruses attacking Java applications. Java program cannot harm
other system thus making it secure.
3) Java is ROBUST
Java encourages error-free programming by being strictly typed and performing run-time checks. Java guides
programmer toward reliable programming habits for creating highly reliable applications.
4) Java is HIGH-PERFORMANCE
Java code is compiled into bytecode which is highly optimized by the Java compiler, so that the Java virtual
machine (JVM) can execute Java applications at full speed. In addition, compute-intensive code can be re-written in
native code and interfaced with Java platform via Java Native Interface (JNI) thus improve the performance.
Like C++ java provides most of the object-oriented features. Java is pure OOP. Language, while C++ is semi
object oriented. It has all OOP features such as abstraction, encapsulation, inheritance and polymorphism. Java
provides the basic object technology of C++ with some enhancements and some deletions.Java is DYNAMIC
Encapsulation is a technique
Creating an interface is also the process
that packages related data
of encapsulation.
and behaviors into a single
unit. This interface groups
the essential behaviors of human-being in a single unit. encapsulation is a technique for protecting data from
misuse by the outside world, which is referred as ‘information hiding’ or ‘data hiding’. For example:
Inheritance is the ability of a class inherits data and behaviors from another class. Note that only public and
protected members of the superclass are inherited by the subclass. The subclass can freely add new members to
extend
features of
This class models a real car (abstraction)
the which has 4 wheels and can carry up to 4
superclass. persons. It has basic behaviors of a standard
car (encapsulation) such as start, stop, gear,
turn and brake.
Polymorphism means “many forms”. In OOP, polymorphism means a type can point to different object at different
time. In other words, the actual object to which a reference type refers, can be determined at runtime. In Java,
polymorphism is based on inheritance and overriding.
Here, the keyword extends is used to make a class inherits data and behaviors of
another class. This is called Inheritance. The Truck class inherits all the
characteristics and behaviors of a car.
The JRE (Java Runtime Environment) smoothed over the diversity of operating systems, ensuring that Java
programs can run on virtually any OS without modification. It also provides value-added services. Automatic
memory management is one of the JRE's most important services, ensuring that programmers don't have to
manually control the allocation and reallocation of memory.
In short, the JRE is a sort of meta-OS for Java programs. It's a classic example of abstraction, abstracting
the underlying operating system into a consistent platform for running Java applications.
The Java Runtime Environment is the on-disk program that loads Java applications for the JVM to execute.
A JRE is included by default when you download the Java Development Kit, and each JRE includes the core Java
class libraries, a Java class loader, and a Java Virtual Machine. It's helpful to understand how the JVM, JDK and JRE
interact, especially for working in cloud and devops environments. In these environments, the JRE takes a stronger
role in monitoring and configuration than it would in traditional Java application development.
JRE Components
The JRE is the software environment in which programs compiled for a typical JVM implementation can run.
The runtime system includes:
• Code necessary to run Java programs, dynamically link native methods, manage memory, and handle
exceptions
• Implementation of the JVM
The following figure shows the JRE and its components, including a typical JVM implementation's various modules
and its functional position with respect to the JRE and class libraries.
The JRE contains libraries and software that your Java programs need to run. As
an example, the Java class loader is part of the Java Run time Environment. This
important piece of software loads compiled Java code into memory and connects the
code to the appropriate Java class libraries.
Installing and using the JRE
While there is a conceptual side
to the JRE, in real-world practice it's just
software installed on a computer,
whose purpose is to run your Java
programs. As a developer, you'll mostly
work with the JDK and JVM, because
those are the platform components you
use to develop and run your Java
programs. As a Java application user,
you would be more involved with the
JRE, which lets you run those programs.
• Metaspace is where Java keeps your program's unchanging info like class definitions.
• Heap space is where Java keeps variable content.
• Stack space is where Java stores function execution and variable references.
•
Variable and Variables Declaration
Variables are the identifier of the memory location, A variable is a container which holds the value while
which used to save data temporarily for later
2) Instance Variable
A variable declared inside the class but outside the b ody
of the method, is called instance variable. It is not
declared as static.
It is called instance variable because its value is instance
specific and is not shared among instances.
3) Static variable
EXAMPLE :
A variable which is declared as static is called static variable. It
cannot be local. You can create a single copy of static variable and share among
all the instances of the class. Memory allocation for static variable happens only
once when the class is loaded in the memory.
In this example, we have declared three variables using var keyword: one, two and three. We have
assigned values to variables one and two at the
same time when we declared it, whereas variable
three is declared but does not hold any value yet,
so it's value will be 'undefined'.
Points to Remember :
Variable stores a single data value that
can be changed later.
Variables can be defined
using var keyword. Variables defined
without var keyword become global variables.
Variables must be initialized before using.
Multiple variables can be defined in a single
line. e.g. var one = 1, two = 2, three = "three";
Variables in JavaScript are loosely-typed
variables. It can store value of any data type throughout its life time.
Data types specify the different sizes and values that can be stored in the variable. There are two types of data
types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and
double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.
In Java language, primitive data types are the building blocks of data manipulation. These are the most
basic data types available in Java language.
An identifier is the representation within the language of items created by the user, as opposed to
language keywords or commands. Some identifiers stand for dictionary objects, which are the objects you create-
such as tables, views, indexes, columns, and constraints- that are stored in a database. They are called dictionary
objects because Derby stores information about them in the system tables, sometimes known as a data dictionary.
SQL also defines ways to alias these objects within certain statements.
Comments Within SQL Statements
Comments can make your application easier for you to read and maintain. For example, you can include
a comment in a statement that describes the purpose of the statement within your application. With the exception
of hints, comments within SQL statements do not affect the statement execution. Please refer to "Hints " on using
this particular form of comment.
A comment can appear between any keywords, parameters, or punctuation marks in a statement. You can
include a comment in a statement in two ways:
ADMIN CURSOR FOUND MOUNT
• Begin the comment with a slash and an
asterisk (/*). Proceed with the text of the AFTER CYCLE FUNCTION NEXT
comment. This text can span multiple lines. ALLOCATE DATABASE GO NEW
End the comment with an asterisk and a ANALYZE DATAFILE GOTO NOARCHIVELOG
slash (*/). The opening and terminating ARCHIVE DBA GROUPS NOCACHE
characters need not be separated from the
ARCHIVELOG DEC INCLUDING NOCYCLE
text by a space or a line break.
• Begin the comment with -- (two hyphens). AUTHORIZATION DECLARE INDICATOR NOMAXVALUE
Proceed with the text of the comment. This AVG DISABLE INITRANS NOMINVALUE
text cannot extend to a new line. End the BACKUP DISMOUNT INSTANCE NONE
comment with a line break.
BEGIN DOUBLE INT NOORDER
BECOME DUMP KEY NORESETLOGS
Some of the tools used to enter SQL have additional
restrictions. For example, if you are using SQL*Plus, BEFORE EACH LANGUAGE NORMAL
by default you cannot have a blank line inside a BLOCK ENABLE LAYER NOSORT
multiline comment. For more information, please BODY END LINK NUMERIC
refer to the documentation for the tool you use as an
CACHE ESCAPE LISTS OFF
interface to the database.
CANCEL EVENTS LOGFILE OLD
The following words also have a special meaning to CASCADE EXCEPT MANAGE ONLY
Oracle but are not reserved words and so can be CHANGE EXCEPTIONS MANUAL OPEN
redefined. However, some might eventually become CHARACTER EXEC MAX OPTIMAL
reserved words.
CHECKPOINT EXPLAIN MAXDATAFILES OWN
CLOSE EXECUTE MAXINSTANCES PACKAGE
Definition - What does Keyword mean?
COBOL EXTENT MAXLOGFILES PARALLEL
In Java, a keyword is a word with a predefined COMMIT EXTERNALLY MAXLOGHISTORY PCTINCREASE
meaning in Java programming language syntax.
COMPILE FETCH MAXLOGMEMBERS PCTUSED
Reserved for Java, keywords may not be used as
identifiers for naming variables, classes, methods or CONSTRAINT FLUSH MAXTRANS PLAN
other entities. CONSTRAINTS FREELIST MAXVALUE PLI
CONTENTS FREELISTS MIN PRECISION
There are 50 reserved keywords in the Java CONTINUE FORCE MINEXTENTS PRIMARY
programming language. Because these keywords
CONTROLFILE FOREIGN MINVALUE PRIVATE
have predefined functions, they are usually
highlighted with different colors in most integrated COUNT FORTRAN MODULE PROCEDURE
development environments (IDE) used by Java
programmers.
Common Java keywords include abstract, assert, boolean, break, byte, case, catch, char, class, const, continue,
default, do, for, while, switch, this and int. The keywords const and goto are reserved but not currently used by
the Java language. False, null and true are reserved
keywords for literal values
Java operators are symbols that are used to perform mathematical or logical manipulations. Java is rich with
built-in operators. Operators are tokens that perform some calculations when they are applied to variables.
val = ++a; Increments "a" then store the new value of "a" in "val".
val = --a; Decrements "a" then store the new value of "a" in "val".
• Relational Operators
Operator Meaning
== Is equal to
!= Is not equal to
&& Logical If both operands are true then only "logical AND operator" evaluate true.
AND
|| Logical The logical OR operator is only evaluated as true when one of its operands
OR evaluates true. If either or both expressions evaluate to true, then the result is
true.
! Logical Logical NOT is a Unary Operator, it operates on single operands. It reverses the
Not value of operands, if the value is true, then it gives false, and if it is false, then it
gives true.
• Bitwise
Operators
& Binary AND There are two types of AND operators in Java: the logical && and the
Operator binary &. Binary & operator work very much the same as
logical && operators works, except it works with two bits instead of
two expressions. The "Binary AND operator" returns 1 if both
operands are equal to 1.
| Binary OR Like "AND operators ", Java has two different "OR" operators: the
Operator logical || and the binary |. Binary | Operator work similar to
logical || operators works, except it, works with two bits instead of
two expressions. The "Binary OR operator" returns 1 if one of its
operands evaluates as 1. if either or both operands evaluate to 1, the
result is 1.
^ Binary XOR It stands for "exclusive OR" and means "one or the other", but not
Operator both. The "Binary XOR operator" returns 1 if and only if exactly one
of its operands is 1. If both operands are 1, or both are 0, then the
result is 0.
~ Binary
Complement
Operator
Assignment Operators
• The Java Assignment Operators are used when you want to assign a value to the expression. The
assignment operator denoted by the single equal sign.
• In a Java assignment statement, any expression can be on the right side and the left side must be a variable
name. For example, this does not mean that "a" is equal to "b", instead, it means assigning the value of
'b' to 'a'. It is as follows:
• Syntax:
• variable = expression;
• Example:
• int a = 6;
• float b = 6.8F;
• Conditional Operator
• The Java Conditional Operator selects one of two expressions for evaluation, which is based on the value
of the first operands. It is also called ternary operator because it takes three arguments.
• The conditional operator is used to handling simple situations in a line.
• Syntax:
• expression1 ? expression2:expression3;
• The above syntax means that if the value given in Expression1 is true, then Expression2 will be evaluated;
otherwise, expression3 will be evaluated.
• Example:
• Instance of Operator
• The Java Instance of Operator is used to determining whether this object belongs to this particular (class
or subclass or interface) or not.
• This operator gives the boolean values such as true or false. If it relates to a specific class, then it returns
true as output. Otherwise, it returns false as output.
• Syntax:
• Example:
• class Company {}
• System.out.println("Success.");
• }
• if (c instanceof Employee) {
• Employee b1 = (Employee) c;
• b1.check();
• }
• }
• Employee.view(c);
• }
• }
• Output:
• Success.
• Member Selection or Dot Operator
Widget w = new Widget();
System.out.println(w.price);
System.out.println(w.getPrice());
Creating an object in Java and accessing its features.
Widget w0; Widget* w1 = new widget;
cout << w0.price << endl; cout << w1->price << endl;
cout << w0.getPrice() << endl; cout << w1->getPrice() << endl;
(a) (b)
Creating an object in C++ and accessing its features. Java can only create an object in one way, but C++ has two
ways of creating objects:
• The object is created as an automatic object and its features are accessed using the dot operator
• The object is created as a dynamic object on the heap and its features are accessed with the arrow
operator
In C++, when we need to access a member through a pointer, the dot operator by itself is not sufficient. Earlier in
this chapter we saw examples involving simple data types that required us to dereference a pointer to access the
contents of the referenced memory address. We can take the same approach when accessing members:
dereference the pointer first and then access the member with the dot operator:
• Alternative syntax for accessing features through a pointer. The arrow operator is a convenience or
"shortcut" operator that combines the dereference and member selection operations into a single
operator. The dot operator has a higher precedence than the indirection operator, which means that the
parentheses are required. Although this syntax works, the arrow operator provides a cleaner, more easily
used syntax and is more common.