0% found this document useful (0 votes)
8 views

'Java Crash Cource Book'

The document provides an overview of programming, focusing on the definition of programs, programming languages, and their types, including machine language, assembly language, and high-level languages. It explains the structure of a Java program, including classes, methods, access modifiers, and naming conventions, as well as the history and features of Java as an object-oriented programming language. Additionally, it outlines the hardware requirements for running Java programs and the steps to install the Java Development Kit (JDK) and Eclipse IDE.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

'Java Crash Cource Book'

The document provides an overview of programming, focusing on the definition of programs, programming languages, and their types, including machine language, assembly language, and high-level languages. It explains the structure of a Java program, including classes, methods, access modifiers, and naming conventions, as well as the history and features of Java as an object-oriented programming language. Additionally, it outlines the hardware requirements for running Java programs and the steps to install the Java Development Kit (JDK) and Eclipse IDE.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Program and programming languages

A program is a set of instructions that tells a program what to do. We use


programs to interact or talk to computers. So , in order to tell the computer
to do something we must write a program. To write programs we use
programming languages.

Basically , programming languages are used to write programs. As you


know, computers are machines and they do not understand human
languages. So, programs are written in a language that computers can
understand.

Machine Languages: This is the computer’s native language which it uses


comprising only 0 and 1. This is why it is called as a binary language. This
language is machine dependent , i.e , it differs among different types of
machines. For example , a binary code for writing 5 on one machine could
be 1101101010011010 , but this might be different on some other machine.

Every instruction must be written in machine language before it is


executed. Later in this book we are going to use Java language for writing
our codes. All the instructions written in same programming language must
be translated to machine code instructions.

Assembly language: This language was developed to make programming


easier. It is also machine dependent. In this language some keywords like
add , sub … were introduced. There is a program called “assembler” that
translates assembly code to machine code.

Assembler: The code given to the assembler is translated by the


assembler to machine code. For example,

Add 2,3,result Assembler


110110101001010

The code given to the assembler cannot be executed directly but after it is
translated it can be easily executes.
High -level language: this is the new generation of programming
languages in which we use English words to write code making it easier for
us to write our code. It is easy to learn and use. It is machine independent

Page 1 of 17
so that your program will run on different. Each language instruction is
called a ‘statement’. A program written in high level language is called a
‘source code' or ‘source program'. In this language to add 2 and 3, we must
write (in Java only):
Result = to 2 + 3;
Here we are assigning result to store a sum of two and three which is 5 but
the way of writing a code is very easy to calculate 2 + 3 and allocates it to
to the memory of ‘result’.

As in mathematics we have a concept of variable here also we have that


‘result’ as a variable(variables will be covered in further chapters).you might
have noticed that in the above example we have ended the statement with
a semicolon, this is because almost every sentence in Java ends with a
semicolon.

As an assembler is used to translate assembly language into machine


code, a ‘compiler’ or ‘interpreter’ is used to translate a source code into
machine code.

A compiler translates source code to machine code which is executed by


executor to give the desired output or result.
An interpreter translates each statement into machine code and execute it
right away. It directly give the output of a statement without any
involvement of the executor.

Page 2 of 17
Anatomy of a Java program

A class is a blueprint to create objects and an object is an instance of a


class.
For example, the factory creates several cars of different model. The class
here will be the factory as it has all the characteristics of cars. A car is an
object of class which is the factory.

Class structure

Class class_name{
Code block
}

We will use a class keyword and then we will put under scroll and then
specify the name of our class (names can be anything). After this put a pair
of curly braces inside which we will type a code.

Methods
These are a group of instructions to do a specific task. We cannot create
method without a class.

In Java we have a special method called ' main () '.

Syntax –
return_type method_name(parameters){
Code block
}

For calling a method we must write:


Method_name(parameters);
Ex – SayHi(Tom);

After calling a method, the method is executed. In this case 'Hi Tom' will be
printed.

Page 3 of 17
main() is automatically called when we run a Java program. It is the starting
point of execution of a Java program. All the other methods are called in
the main () method only. We cannot have a Java program without a
main method.
So why now you might have got the idea that whenever we need to create
a method it must be contained in a class.

Access Modifiers

These are the keywords with specified how to use classes and methods.
We have the following keywords (Access Modifiers) in Java –

Public
Private
Protected
Default

The above keywords are used in the basic Java program. These will be
discussed in detail later in this book. For now just keep in mind that in Java
we have public, private, protected and default keywords. These keywords
are access modifiers in Java.

Naming Conventions

These naming conventions are common in programming languages and


not only in Java. The specify how to write names in programming. We have
got called the Pascal case convention:
Suppose we are typing a name in Java and it is something like this –
ThisIsAName

Using the pascal case convention every letter of the first word will be
capital.this convention is used for classes in Java. So, we must use Pascal
case convention for writing the names of our classes in Java.

Also we have the


Camel case convention:
Accoeding to this convention the first letter of the starting word is small or is
in lowercase but then the first letter of the next word is capital.this is similar
to the pascal case convention but only the difference is that the first letter of

Page 4 of 17
the starting word must be in lower case.This type of convention is used for
naming variables and methods in Java. We will talk about variables later
but what I want to make you know is that we use this type of convention in
naming variables.
Example –
thisIsAName

Then the next convention is


Snake case convention:
in this convention between every word there is under score.
Example –
this_is_a_name

Java program structure

Every Java program contains at least one class.


Example –

Public class Main{

So first of all we write the access modifier which is public and this case and
then write the class keyword after which we write the name of our class and
curly braces. All our code would be written in the parentheses. As we know
we have to create a main method in every class so we have to write the
code for main method which is as follows

public class Main {


public static void main (String[]args){

Here we have a public key word which is access modifier than static
keyword which is non access modifier(we will talk about non access
modifiers later in OOPs).void is the return type, then we put the name, then
we open parentheses inside which we have parameters.(String[]args) is the
data that we can give the main method to work with. After this we open the
Page 5 of 17
curly braces in which we write our code to be executed when are main
method runs, this is called the body of the main method. This is a simple
java program and when we create our first program you will find that
program exactly like this.
The name of our class is named according to pascal case convention
and the name of our method is named according to camel case
convention.
We cannot change the name of the main method, all its letters must
be in lowercase.

Packages

A package is a container of classes in Java. Inside a package we can have


multiple classes

Page 6 of 17
Introduction and History of Java
Java is an object-oriented programming language developed by James
Gosling and colleagues at Sun Microsystems in the early 1990s. Unlike
conventional languages which are generally designed either to be compiled
to native (machine) code, or to be interpreted from source code at runtime,
Java is intended to be compiled to a bytecode, which is then run by a Java
Virtual Machine.Most of the syntaxes of Java are similar to that of C and C+
+.

Java was started as a project called "Oak" by James Gosling in June 1991.
Gosling's goals were to and simplicity than C/C++. The first public
implementation was Java 1.0 in 1995. It made the promise of "Write Once,
Run Anywhere", with free runtimes on popular platforms. It was fairly
secure and its security was configurable, allowing for network and file
access to be limited. The major web browsers soon incorporated it into
their standard configurations in a secure "applet" configuration. popular
quickly. New versions for large and small platforms (J2EE and J2ME) soon
were designed with the advent of "Java 2". Sun has not announced any
plans for a "Java 3".

In 1997, Sun approached the ISO/IEC JTC1 standards body and later the
Ecma International to formalize Java, but it soon withdrew from the
process. Java remains a proprietary de facto standard that is controlled
through the Java Community Process. Sun makes most of its Java
implementations available without charge, with revenue being generated by
specialized products such as the Java Enterprise System. Sun
distinguishes between its Software Development Kit (SDK) and Runtime
Environment (JRE) which is a subset of the SDK, the primary distinction
being that in the JRE the compiler is not present.

There were five primary goals in the creation of the Java language:

1. It should use the object-oriented programming methodology.

Page 7 of 17
2. It should allow the same program to be executed on multiple operating
systems.
3. It should contain built-in support for using computer networks.
4. It should be designed to execute code from remote sources securely.
5. It should be easy to use by selecting what was considered the good
parts of other object-oriented languages.

To achieve the goals of networking support and remote code execution,


Java programmers sometimes find it necessary to use extensions such as
CORBA, Internet Communications Engine, or OSGi.

Primary goal of Object Oriented programming is to develop more generic


objects so that software can become more reusable between projects. A
generic "customer" object, for example, should have roughly the same
basic set of behaviors between different software projects, especially when
these projects overlap on some fundamental level as they often do in large
organizations. In this sense, software objects can hopefully be seen more
as pluggable components, helping the software industry build projects
largely from existing and well-tested pieces, thus leading to a massive
reduction in development times. Software reusability has met with mixed
practical results, with two main difficulties: the design of truly generic
objects is poorly understood, and a methodology for broad communication
of reuse opportunities is lacking. Some open source communities want to
help ease the reuse problem, by providing authors with ways to
disseminate information about generally reusable objects and object
libraries.

One idea behind Java's automatic memory management model is that


programmers should be spared the burden of having to perform manual
memory management. In some languages the programmer allocates
memory to create any object stored on the heap and is responsible for later
manually deallocating that memory to delete any such objects. If a
programmer forgets to deallocate memory or writes code that fails to do so
in a timely fashion, a memory leak can occur: the program will consume a
potentially arbitrarily large amount of memory. In addition, if a region of
memory is deallocated twice, the program can become unstable and may
crash. Finally, in non garbage collected environments, there is a certain
degree of overhead and complexity of user-code to track and finalize
allocations.

Page 8 of 17
Comparing Java and C++, it is possible in C++ to implement similar
functionality (for example, a memory management model for specific
classes can be designed in C++ to improve speed and lower memory
fragmentation considerably), with the possible cost of extra development
time and some application complexity. In Java, garbage collection is built-in
and virtually invisible to the developer. That is, developers may have no
notion of when garbage collection will take place as it may not necessarily
correlate with any actions being explicitly performed by the code they write.
Depending on intended application, this can be beneficial or
disadvantageous: the programmer is freed from performing low-level tasks,
but at the same time loses the option of writing lower level code.

The syntax of Java is largely derived from C++. However, unlike C++,
which combines the syntax for structured, generic, and object-oriented
programming, Java was built from the ground up to be virtually fully object-
oriented: everything in Java is an object with the exceptions of atomic
datatypes (ordinal and real numbers, boolean values, and characters) and
everything in Java is written inside a class.

The Java Runtime Environment or JRE is the software required to run any
application deployed on the Java Platform. End-users commonly use a JRE
in software packages and Web browser plugins. Sun also distributes a
superset of the JRE called the Java 2 SDK (more commonly known as the
JDK), which includes development tools such as the Java compiler,
Javadoc, and debugger.

Features of Java
 Object Oriented
 Platform Independent
 Simple and Secure
 Architectural Neutral
 Portable
 Robust
 Multithreaded
 Interpreted
 High Performance
 Distributed
 Dynamic

Page 9 of 17
Java Environment Setup
There are certain requirements of hardware to run a java program.

Hardware Requirements (These are the minimum requirements):


 Computer with minimum 64 MB of RAM.
 Linux 7.1 or windows xp/7/8 operating system.
 Microsoft Notepad , eclipse or IntelliJ Idea or any other editor.
 Java JDK 8.

Steps to install JDK:


 Open any browser.
 Search “Java JDK free download “
 Open the one which is offered to you by oracle.com
 Download the latest version which is available according to
your system like Windows x64/x32 bit
 An exe file will be installed
 Run the exe file
 You are now good to go.

After installing the JDK you need to set the path in your computer. Here are
the steps:
 Right click on ‘My Computer’ and select “Properties”
 Click on the ‘Environment variables’ button under the
‘Advanced’ tab.
 Now , alter the path variable so that it also contains the path to
the Java executable. Example, if your path is currently set to

Ç:\WINDOWS\SYSTEM32’ then change your path to Ç:\


WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin’

Now after installing the JDK and setting the path , you must install Eclipse
IDE or you can also use any other IDE like IntelliJ or even Notepad.

Steps to download Eclipse :

Page 10 of 17
 Open any browser.
 Search ‘free download eclipse ide’
 Open the one which is provided to you by eclipse.com
 Download the version according to your system like x64/x32 bit
 An exe file will be installed
 Run the file
 Here you have to select Éclipse IDE for java developers’
 Click on install.
 Now you are good to go.

Basic syntax of Java


 Java is a case sensitive language which means whether you have to
write a particular word in capital case or in lower case.

Page 11 of 17
 Identifiers are the names of variables , methods , classes, packages
and interfaces(all of these will be covered in further chapters).
 A whitespace is a term used in Java to describe blanks , tabs ,
newline characters and comments. These are totally ignored by the
java compiler. Example – int age;
The space between “int” and “age” is called a whitespace.
 Comments are like helping text in a Java program. In real world
scenario , there are many people work for the same project , hence
writing comments helps other people understand what you have
written.
There are two types of comments :
1.Single comments are denoted by 2 ” // ” slashes.

2.Multi comments are denoted by “/*“ and “*/” where “/*“ is used
to denote start and the other is used to denote end.

3.Multi line comments also have a format of starting with “/**”


and ending with “*/” , but this is used for automatic
document generation means in simple words these are done by
the JDK itself.

Basic structure of a Java program

1.Documentation section (optional): It contains the details like when the


program was created, who is its author, etc. The things in this section are
written in the form of comments , hence it can also be called as “a group of
comments”.

2.Package statements (optional):If in a program we are using package(s)


, then this statement comes in use. All the classes defined below a
package will be belonging to that package. For example,
package Student;

3.Import statements (optional): By this statement we can import a


particular class of may be some other package into our program. For
example , if we have a package named “test” inside which we have a class
named “Student” , then the code goes like - import test.Student;

Page 12 of 17
By the above statement we can only use the methods of the class Student
but not all the classes present in the test package. To do that we must write
the code as - import test.*;

4.Interface statements (optional): These statements are used when we


have to use multiple inheritance (inheritance is explained in further
chapters). These statements are just like a class but in this the methods
are declared (methods are explained in further chapters).

5.Class Definitions: If there is a usage of multiple classes in our program


the declaration of those classes is done in these definitions. Here you can
define the characteristics of classes like what variables they will contain ,
what will be the methods in those classes , etc.

6.Main Class Definition


{
Main method definition
} : Without this your code cannot be compiled , so it is the most essential
part of a code. The entry and exit of a program is by a main method.

Variables and Data Types in Java

Page 13 of 17
You can think of variables as container. this is because in programming
when you talk about software it is all about data. Hence, when you build
software you are processing a lot of data. Now the question arises where to
store the data? the answer is we have a special kind of thing which is
called variable for storing our data. This basic concept is given in maths it is
the same here also you can even change the value of the data stored in a
variable.

For example if you want to store a pencil, you can keep it in a pouch, in
your bag, in your pocket or somewhere else. So these things like your
pocket, pouch or your bag are variables.

For different types of values we have different variables.

Data type Keyword Size Size Min Max


(bit) (byte) value value
Character char 16 2 0 65535
Byte byte 8 1 -128 127
short short 16 2 - 32767
32768
integer int 32 4
float float 32 4
long long 64 8
double double 64 8
boolean boolean NA NA

From the table you will come to know that different values of data must be
stored and different types of data types. For example if you want to store a
value of let say 124 , the data type that you must use is character here, you
can use integers also. I don't prefer using integer for such a small number
because it it requires more space than characters.

For assigning a value to the variable first we must write the data type that
we want to use then write the name of the variable that we need but it could
be anything after which we have to put a equals to sign and specify the
value that we want our variable to get assigned. For example -
char a = 8;

Page 14 of 17
Here char is a data type, a is the name of variable and 8 is the value
assigned.
We can assign a integer value to a float value and also to double value but
we cannot assign a double value to integer value. It is just like putting a
pouch in a pen. For example if you want to store a double value let's say
2.4 in the integer data type then we have to lose some data , here we will
lose .4 . So, the final integer value would be to 2. These all data types are
called as primitive datatypes.

variables can be re-used that is one I declare a variable I can use it again
and again in my program and I can even modify it afterwards in the
program. For example if I create a variable a which is containing a value 5
and 8 then I can reuse it by printing the variable a ,i.e.,

System.out.println(a); // output - 5

In Java System.out.print or System.out.println is used to print statement in


Java.

Now if you want to assign a value, let say 5, to a variable using the integer
data type then it will then it will consume 4 bytes of your memory instead
you can use a term called as short which basically means that it is a short
integer as it requires only to bytes of your memory. The range of a short is -
32768 to 32767. Using this short you can save to bytes of your memory.

Now using short for storing the value 5 would also would also be more
space consuming, to solve this problem you can use byte which takes only
one byte of your memory. The range for byte is –128 to 127.

If you have a number which is let say is of 8 to 10 digits then you have to
use long. when you initialize a number to long data type then you have to
write a letter L after the number. For example, you can write long x =
500000000L;

For storing a single character in a variable such as P we use the data type
called as char. It is used to assign the value of a single character only.
For example – char x = 'D’;

Page 15 of 17
If we store the value 5 using a double data type then it would be
automatically converted into a double For example , double b5 = 5; . This
is called as implicit conversion where the value automatically gets
converted into other data type. If we see the output of the above example
then we will get the output 5.0 this is because double supports float values
as well. So if we assign it to integer value it would show point zero as well.

If we assign a value let se 6.1 to an integer then it would show an error


because and feature does not support float values, if you want to store float
values into integer then we must cast the value, for example , int a =
(int)6.1; if you look at the output of the statement then we find that the
output is 6 this is because we are storing 6.1 as an integer. This type of
convergence are known as explicit conversions.

Byte values can be casted into short , short and character values can be
assigned to an integer, integer can be assigned to long, long can be
assigned to float data type and float data type can be assigned to a double
data type.

Page 16 of 17
Operators in Java

Page 17 of 17

You might also like