KEMBAR78
Java I and III | PDF | Java (Programming Language) | Class (Computer Programming)
0% found this document useful (0 votes)
34 views9 pages

Java I and III

Thank you

Uploaded by

www.preethi0114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Java I and III

Thank you

Uploaded by

www.preethi0114
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Java is a popular, high-level, object-oriented programming language that was

developed by James Gosling and his team at Sun Microsystems (now owned by
Oracle Corporation) in the mid-1990s. It is widely used for developing various
kinds of software, including web applications, desktop applications, mobile
applications, games, and more.
Step 1: Install Java Development Kit (JDK) on your computer. You can download
the JDK from the Oracle website or any other reliable source.
Step 2: Write your Java code in a text editor such as Notepad or any other code
editor. Save the file with a .java extension and file type as ‘All files’ in the
desired location.
Step 3: Open the command prompt (Windows) or terminal (Mac or Linux).
Step 4: Navigate to the directory where you saved your Java code using the
“cd” command.
Step 5: Compile your Java code by typing “javac [filename].java” in the
command prompt/terminal. This will create a .class file in the same directory.
Step 6: Run your Java program by typing “java [filename]” in the command
prompt/terminal. The program will execute and produce the output.
What is Java?
Java is a popular programming language, created in 1995.
It is owned by Oracle, and more than 3 billion devices run Java.
It is used for:
 Mobile applications (specially Android apps)
 Desktop applications
 Web applications
 Web servers and application servers
 Games
 Database connection
 And much, much more!
Why Use Java?
 Java works on different platforms (Windows, Mac, Linux, Raspberry Pi,
etc.)
 It is one of the most popular programming languages in the world
 It has a large demand in the current job market
 It is easy to learn and simple to use
 It is open-source and free
 It is secure, fast and powerful
 It has huge community support (tens of millions of developers)
 Java is an object oriented language which gives a clear structure to
programs and allows code to be reused, lowering development costs
 As Java is close to C++ and C#, it makes it easy for programmers to switch
to Java or vice versa.

Java, a popular and versatile programming language, offers various ways to


develop and deploy applications. Two common methods of creating Java
programs are
 standalone applications
 applets.
Standalone applications, also known as desktop applications or Java
applications, are programs that run independently on a user's computer. They
are self-contained and don't require a web browser or any other platform to
execute. Standalone applications are typically developed for various operating
systems (OS) like Windows, macOS, and Linux, thanks to Java's "write once, run
anywhere" principle.
Applets are Java programs that are designed to run within web browsers. They
were popular in the early days of the internet and provided a way to add
interactive content to web pages. However, due to security concerns and the
evolution of web technologies, applets have become less common.

// Your First Program


class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
In Java, any line starting with // is a comment. Comments are intended for
users reading the code to understand the intent and functionality of the
program
In Java, a class declaration defines a new class, which is a blueprint for creating
objects. Every line of code that runs in Java must be inside a class. And the
class name should always start with an uppercase first letter. In our example,
we named the class HelloWorld.
Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning.
The name of the java file must match the class name. When saving the file,
save it using the class name and add "HelloWorld.java" to the end of the
filename.
public
 Access modifier: It means this method is accessible from anywhere,
including outside the class.
 Required so the Java Virtual Machine (JVM) can call it from outside the
class.
static
 Means you don't need to create an instance of the class to call this
method.
 The JVM calls this method without creating an object of the class.

void
 This method returns nothing.
main
 This is the name of the method that serves as the starting point of a Java
program.
 The JVM looks for this method when running your program.
String[] args
 A parameter: it's an array of strings.
 It stores command-line arguments passed to the program when it starts.
JAVA PROGRAM STRUCTURE
A Java program may contain many classes of which only one class will have a
main method. The class will contain data members and methods that operate
on the data members of the class.

Documentation You can write a comment in this section. Comments


Section are beneficial for the programmer because they help
them understand the code. These are optional, but
we suggest you use them because they are useful to
understand the operation of the program, so you
must write comments within the program.
Package statement You can create a package with any name. A package is
a group of classes that are defined by a name. That is,
if you want to declare many classes within one
element, then you can declare it within a package. It
is an optional part of the program, i.e., if you do not
want to declare any package, then there will be no
problem with it, and you will not get any errors. Here,
the package is a keyword that tells the compiler that
package has been created.
Package student;
Import statements This line indicates that if you want to use a class of
another package, then you can do this by importing it
directly into your program.
Interface statement Interfaces are like a class that includes a group of
method declarations. It's an optional section and can
be used when programmers want to implement
multiple inheritances within a program.
Class Definition A Java program may contain several class definitions.
Classes are the main and essential elements of any
Java program
MainMethod Class Every Java stand-alone program requires the main
method as the starting point of the program. This is
an essential part of a Java program. There may be
many classes in a Java program, and only one class
defines the main method. Methods contain data type
declaration and executable statements

Java Tokens
In Java, Tokens are the smallest elements of a program that is meaningful to
the compiler. They are also known as the fundamental building blocks of the
program. Tokens can be classified as follows:
1. Keywords
2. Identifiers
3. Constants/Literals
4. Operators
5. Separators
1.Keyword
Keywords are pre-defined or reserved words in a programming language. Each
keyword is meant to perform a specific function in a program. they can’t be
used as variable names. Java language supports the following keywords
example:
Abstract, do, for, int , private, public, short
2. Identifiers
Identifiers are used as the general terminology for naming of variables,
functions and arrays. These are user-defined names consisting of an arbitrarily
long sequence of letters and digits with either a letter or the underscore (_) as
a first character. Identifier names must differ in spelling and case from any
keywords. You cannot use keywords as identifiers; they are reserved for special
use. Once declared, you can use the identifier in later program statements to
refer to the associated value. A special kind of identifier, called a statement
label, can be used in goto statements. Examples of valid identifiers:
MyVariable, MYVARIABLE, myvariable, x, I, x1,
3. Constants/Literals
Constants are also like normal variables. But the only difference is, their values
cannot be modified by the program once they are defined. Constants refer to
fixed values. They are also called as literals. Constants may belong to any of the
data type. Syntax:
final data_type variable_name;

import java.io.*;
class GFG {
public static void main (String[] args) {

// Here final keyword is used


// to define the constant PI
final double PI = 3.14; // Use double instead of int
// Example usage of PI
System.out.println("The value of PI is: " + PI);
}
}

4. Operators
Java provides many types of operators which can be used according to the
need. They are classified based on the functionality they provide. Some of the
types are-
 Arithmetic Operators
 Unary Operators
 Assignment Operator
 Relational Operators
 Logical Operators
 Ternary Operator
 Bitwise Operators
 Shift Operators
 instance of operator
 Precedence and Associativity
5. Separators
Separators are used to separate different parts of the codes. It tells the compiler
about completion of a statement in the program. The most commonly and
frequently used separator in java is semicolon (;).

int variable; //here the semicolon (;) ends the declaration


of the variable

import java.io.*;
class GFG {
public static void main (String[] args) {
//Here the semicolon (;) used to
// end the print statement
System.out.println("GFG!");
}
}

Java Statements
In Java, a statement is an executable instruction that tells the compiler what to
perform. It forms a complete command to be executed and can include one or
more expressions.
Declaration Statements
In declaration statements, we declare variables and constants by specifying
their data type and name. A variable holds a value that is going to use in the
Java program. For example:
int quantity;
boolean flag;
String message;
Also, we can initialize a value to a variable. For example:
int quantity = 20;
boolean flag = false;
String message = "Hello";

Control Statement
Control statements decide the flow (order or sequence of execution of
statements) of a Java program. In Java, statements are parsed from top to
bottom. Therefore, using the control flow statements can interrupt a particular
section of a program based on a certain condition.

Empty Statement
an empty statement does not have anything barring semicolon (;).
for(int i = 0 ; i < 10 ; i++)
{
// empty body
}
Expression statements
Expression is an essential building block of any Java program. Generally, it is
used to generate a new value. Sometimes, we can also assign a value to
a variable. In Java, expression is the combination of values,
variables, operators, and method calls. Seven types of expression statements
are Assignment, pre-increment, post-increment, post-decrement, method call
and allocation expression.
Example: count++, number = 90, pi = 3.14, sum = a + b

Synchronization statements
These are used for handling issues with multithreading.

Guarding Statements
Used for safe handling of code that may cause exceptions. These statements
use try and catch and finally.

You might also like