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

Java (1) Lectures

The document provides an introduction to the Java programming language. It discusses what Java is, why it should be learned, its key features, environment including JDK, JRE and JVM, popular editors like Netbeans and Eclipse. It also covers Java basics like syntax, variables, data types, comments and escape sequences.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Java (1) Lectures

The document provides an introduction to the Java programming language. It discusses what Java is, why it should be learned, its key features, environment including JDK, JRE and JVM, popular editors like Netbeans and Eclipse. It also covers Java basics like syntax, variables, data types, comments and escape sequences.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming Methods

Introduction to java
What is Java?
 Java is popular high-level, class-based object
oriented programming language originally
developed by Sun Microsystems and released
in 1995.
 Currently Java is owned by Oracle and more
than 3 billion devices run Java.
 Java runs on a variety of platforms, such as
Windows, Mac OS, and the various versions
of UNIX.
 Java is used to develop Mobile apps, Web
apps, Desktop apps, Games and much more.
Why to Learn Java?
 Java is a MUST for students and working
professionals to become a great Software
Engineer specially when they are working
in Software Development Domain.
 Java is fairly easy to learn, so if you are
starting to learn any programming
language then Java could be your great
choice.
Why to Learn Java?
 There are many other good reasons which
makes Java as the first choice of any
programmer:
◦ Java is Open Source which means its available
free of cost.
◦ Java is simple and so easy to learn
◦ Java is much in demand and ensures high salary
◦ Java has a large vibrant community
◦ Java has powerful development tools
◦ Java is platform independent
JAVA Features
 Multithreaded − With Java's multithreaded feature it is possible
to write programs that can perform many tasks simultaneously. This
design feature allows the developers to construct interactive
applications that can run smoothly.
 Interpreted − Java byte code is translated on the fly to native
machine instructions and is not stored anywhere. The development
process is more rapid and analytical since the linking is an
incremental and light-weight process.
 High Performance − With the use of Just-In-Time compilers, Java
enables high performance.
 Distributed − Java is designed for the distributed environment of
the internet.
 Dynamic − Java is considered to be more dynamic than C or C++
since it is designed to adapt to an evolving environment. Java
programs can carry extensive amount of run-time information that
can be used to verify and resolve accesses to objects on run-time.
Java - Environment
Java - Environment
 JDK(Java Development Kit): JDK is intended for
software developers and includes development
tools such as the Java compiler, Javadoc, Jar, and a
debugger.
 JRE(Java Runtime Environment): JRE contains the
parts of the Java libraries required to run Java
programs and is intended for end-users. JRE can
be viewed as a subset of JDK.
 JVM: JVM (Java Virtual Machine) is an abstract
machine. It is a specification that provides a
runtime environment in which java bytecode can
be executed. JVMs are available for many
hardware and software platforms.
Popular Java Editors
 To write your Java programs, you will need a
text editor,you can consider one of the
following :
 Notepad − On Windows machine, you can
use any simple text editor like Notepad
(Recommended for this tutorial), TextPad.
 Netbeans − A Java IDE that is open-source
and free which can be downloaded
from https://www.netbeans.org/index.html.
 Eclipse − A Java IDE developed by the
eclipse open-source community and can be
downloaded from https://www.eclipse.org/
Java - Basic Syntax
 When we consider a Java program, it can be defined as a collection
of objects that communicate via invoking each other's methods. Let
us now briefly look into what do class, object, methods, and
instance variables mean.
 Object − Objects have states and behaviors. Example: A dog has
states - color, name, breed as well as behavior such as wagging their
tail, barking, eating. An object is an instance of a class.
 Class − A class can be defined as a template/blueprint that
describes the behavior/state that the object of its type supports.
 Methods − A method is basically a behavior. A class can contain
many methods. It is in methods where the logics are written, data is
manipulated and all the actions are executed.
 Instance Variables − Each object has its unique set of instance
variables. An object's state is created by the values assigned to
these instance variables.
Basic Syntax
 About Java programs, it is very important to keep in mind
the following points.
◦ Case Sensitivity − Java is case sensitive, which means
identifier Hello and hello would have different meaning in Java.
◦ Class Names − For all class names the first letter should be in
Upper Case. If several words are used to form a name of the
class, each inner word's first letter should be in Upper Case.
 Example: class MyFirstJavaClass
◦ Method Names − All method names should start with a
Lower Case letter. If several words are used to form the name of
the method, then each inner word's first letter should be in
Upper Case.
 Example: public void myMethodName()
◦ Program File Name − Name of the program file should
exactly match the class name.
Java Variables

 Following are the types of variables in


Java:
◦ Local Variables
◦ Class Variables (Static Variables)
◦ Instance Variables (Non-static Variables)
Comments in Java
 Using comments to document your code.
 Multi lines comment:
/* This is my first java program. * This will print
'Hello World' as the output * This is an example
of multi-line comments. */
• Single line comments:
// This is an example of single line comment
Java - Basic Datatypes
 Variables are nothing but reserved memory
locations to store values. This means that when
you create a variable you reserve some space in
the memory.
 Based on the data type of a variable, the
operating system allocates memory and decides
what can be stored in the reserved memory.
 Therefore, by assigning different data types to
variables, you can store integers, decimals, or
characters in these variables.
 There are two data types available in Java :
◦ Primitive Data Types
◦ Reference/Object Data Types
Primitive Data Types
 There are eight primitive datatypes supported by
Java.
 Primitive datatypes are predefined by the
language and named by a keyword.
1. Byte data type is an 8-bit.
 Example: byte a = 100, byte b = -50
2. Short data type is a 16-bit
 Example: short s = 10000, short r = -20000
3. int data type is a 32-bit
 Example: int a = 100000, int b = -200000
4. Long data type is a 64-bit signed two's complement
integer
 Example: long a = 100000L, long b = -200000L
Primitive Data Types
5. Float data type is a single-precision 32-bitIEEE
754 floating point
 Example: float f1 = 234.5f
6. double data type is a double-precision 64-bit
IEEE 754 floating point
 Example: double d1 = 123.4
7. boolean data type represents one bit of
information.
 Example: boolean one = true
8. char data type is a single 16-bit Unicode
character
 Example: char letterA = 'A'
escape sequences
Notation Character represented
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Formfeed (0x0c)
\b Backspace (0x08)
\s Space (0x20)
\t tab
\" Double quote
\' Single quote
\\ backslash
\ddd Octal character (ddd)
\uxxxx Hexadecimal UNICODE character
(xxxx)
Java - Variable Types
 A variable provides us with named storage
that our programs can manipulate.
 Each variable in Java has a specific type,
which determines :
◦ the size and layout of the variable's memory;
◦ the range of values that can be stored within that
memory;
◦ and the set of operations that can be applied to
the variable.
 You must declare all variables before they
can be used.
variable declaration
data type variable [ = value][, variable [ = value] ...] ;

 Here data type is one of Java's data types and variable is the
name of the variable. To declare more than one variable of
the specified type, you can use a comma-separated list.
 Following are valid examples of variable declaration and
initialization in Java .
◦ int a, b, c; // Declares three ints, a, b, and c.
◦ int a = 10, b = 10; //Example of initialization
◦ byte B = 22; // initializes a byte type variable B.
◦ double pi = 3.14159; // declares and assigns a value of PI.
◦ char a = 'a'; // the char variable and it is initialized with value 'a'

You might also like