You are on page 1of 13

CHAPTER 1

• Programming is the process of creating a set  of instructions that tell a


computer how to  perform a task.

• A programming language is a vocabulary and  set of grammatical


rules for instructing a  computer or computing device to perform 
specific tasks

• The structured programming language allows  a programmer


to code a program by dividing  the whole program into smaller
units or  modules.
Also called as modular programming
Procedural programming

Advantages

• Structured programming 
• It is user friendly and easy to understand.
• Similar to English vocabulary of words
and symbols. 
• It is easier to learn. 
• They require less time to write. 
• They are easier to maintain. 
• These are mainly problem oriented rather than  machine
based.
Disadvantages 

• Pointers
• Global data 
• More focus on functions rather than on data

Encapsulation 

• Encapsulation is defined as the wrapping up of data under a


single unit. 
• It is the mechanism that binds together code and the data it
manipulates. 
• It is a protective shield that prevents the data from being
accessed by the code outside this shield.  

Abstraction 

• Data Abstraction is the property by virtue of which only the essential


details are displayed to the user.  
• The trivial or the non-essentials units are not displayed to the
user
Polymorphism 

• One name and many forms 


• Polymorphism in Java is a concept by which we can
perform a single action in different ways. 

INHERITANCE 

• Inheritance in Java is a mechanism in which one object


acquires all the properties and behaviors of a parent
object.  
• The idea behind inheritance in Java is that you can create new
classes that are built upon existing classes.  
• When you inherit from an existing class, you can reuse
methods and fields of the parent class.

SIMPLE

• Java is one of the simplest programming languages to learn


and master in order to grasp the concept of Object-oriented
learning. 
• When Java came into being, a lot of developers were
already working in the then programming language C++.  
• In order to tackle the migration problem, the Java syntax is
actually very similar to the C++ syntax which made it easier for
developers to migrate  from C++ to Java.
OOP 
• This is one of the primary reasons why Java is
so popular amongst developers.  
• Java strictly conforms to the rules of r OOP.
• Object-oriented development includes concepts of
Objects and Classes and many more.
This enables developers to have a wide variety of
options for designing their software. 
Java also supports 
• Polymorphism 
• Inheritance 
• Abstraction 
• Encapsulation

Platform Independent 

• Java does that by having the concept of a Java Virtual


Machine. 
• It is also known as a write-once run anywhere (WORA).
• This is where it beats the other programming languages by a
huge mark.  
• Previously all programming languages generated machine-
level code for the particular machine environment. Hence it
was very difficult for developers to collaborate if they were
using different environments to code the same software. 
• This is where Java came in with a software-oriented
platform that had 2 components namely an API and a
Runtime Environment.
Secure 

• Security is everybody’s prime concern in today’s world.


• Java, unlike other languages, runs all of its programs inside a sandbox
of its own called the Java Virtual Machine so that any errors or crashes
do not harm the  external operating system, thus making it secure and 
efficient at the same time.
• Java, unlike other languages, runs all of its programs inside a sandbox
of its own called the Java Virtual  Machine so that any errors or crashes
do not harm the  external operating system, thus making it secure and 
efficient at the same time.

Robust

• Java is a strong language. 


• Java uses strong memory management techniques so
that there is no space for improper memory
assignment during the running of a program.
• Malicious code can use explicit pointers to access
code which is outside their allowance or restricted,
sensitive data. Hence Java has no support for pointers.

Robust 

•“I ran into an error and my compiler closed immediately” was a huge
problem for developers who were developing applications  before the
advent of a feature called Error Handling.
• Java implemented the concept and allowed users to execute and do
custom actions when the program crashed or reported an error.
Neutral

• Primitive languages were not neutral to the architecture


of development environments.
• For example, C had different sizes of data types for 32-
bit systems and 64-bit systems.
• Java decided to be neutral to all platforms
which dramatically increased collaboration efficiency.
• Java code does not compile its code to platform-
specific byte code, but it’s compiled into platform
independent byte code.
• This means that the generated class file can now run
on different machines running different
environments, different operating systems with the only
requirement of a Java Virtual Machine to be present in each
machine.
• This makes Java an architecturally robust and
flexible programming language.
 
High Performance

• Java’s byte code makes it a lot faster than other languages because of
its innate similarity to native code.
• Native code is the code which is processor specific, i e. it has to be
compiled to run with a specific processor, like Intel’s x86  class
processor.
Multithreaded Programming

• Java supports multithreaded programming i.e., it supports multiple


operations running at the same time.
• We can think of a thread as an individual operation or parts of the
program using the processor.
• It increases the performance by decreasing the development time
needed for particular software.
• The coding for particular software becomes streamlined. The
maintenance cost drops.
• However, all of these processes share the same memory slots as
individual processes use the memory efficiently.

Distributed Language

• It can be used for transfer and execution of  programs by remote


computers on the local  machines from the internet.

Dynamic

• Classes are not loaded all at once.


• They jump into action only when an invoke operation executes or
some data about the class is needed in the memory.
• Java finalizes invoking instructions during runtime.
• Ex- Runtime Polymorphism i.e. functions overriding.
  
JAVA Environment
•The programming environment of Java consists of three components
mainly:

• JDK
• JRE
• JVM

Comment

• In Java there are three types of comments:

• Single – line comments. 


• Multi – line comments.

Comment

• //Comments here (Text in this line only is considered as  comment ) 

class demo 
{  
public static void main(String args[])  
{  
// Single line comment here  
System.out.println("Single line comment
above");  }  
 
}
Comment…Multiline
 
//Java program to show multi line comments class demo  
{  
public static void main(String args[])  
{  
System.out.println("Multi line
comments below");  /*Comment line
1  
Comment line 2  
Comment line 3*/ 
}  

Compiling the program


• javac demo.java  
• The compiler creates a file called  
HelloWorld.class (in present working directory) 
that contains the bytecode version of the 
program.  

• Now, to execute our program, JVM(Java Virtual 


Machine) needs to be called using java,  specifying
the name of the class file on the  command line, as
shown: 

• java demo 
• This will print “Hello World” to the terminal screen.
Java Variables

• Variables are containers for storing data values.


• String - stores text, such as "Hello". String values
are surrounded by double quotes 
• int - stores integers (whole numbers), without 
decimals, such as 123 or -123 
• float - stores floating point numbers, with decimals, such
as 19.99 or -19.99 
• char - stores single characters, such as 'a' or 'B'. Char values
are surrounded by single quotes
• boolean - stores values with two states: true or false

Rules of declaring 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, $ or underscore.
• Blank spaces cannot be used in variable names.
• Java keywords cannot be used as variable names.

• VALID VARIABLE NAMES 


• int x=50; 
• int $x=50; 
• int _x=50;
Declaring (Creating) Variables 

To create a variable, you must specify the type and assign it a


value:

• Syntax 
• type variable = value; 

❑ Example 

• Create a variable called name of type String and assign


it  the value "John": 

String name = "John"; 


System.out.println(name);

Variables 

• You can also declare a variable without assigning the value, and assign
the value later 

• Example 
int a; 
a = 15; 
System.out.println(a);

Final Variables 
• You can add the final keyword if you don't want  others (or
yourself) to overwrite existing values 

• This will declare the variable as "final" or "constant", which


means unchangeable and read only 

❑Example 

final int a = 15; 


a = 20; // will generate an error: cannot assign a value to a
final variable

Variables 

• Note that if you assign a new value to an existing  variable, it will


overwrite the previous value:

❑Example 
• Change the value of a from 15 to 20: 
int a = 15;
a = 20;// myNum is now 20 System.out.println(a);
Data types

• Java has two categories of data: 

• Primitive Data Type: such as boolean, char, int, short,  byte,


long, float and double. 
• Primitive data are only single values and have no 
special capabilities. 
• Non-Primitive Data Type : String, Array, etc.
• Non-Primitive Data Types: These data types are not actually
defined by the programming language but are created by the
programmer. They are also called “reference variables”

You might also like