0% found this document useful (0 votes)
6 views33 pages

Java Programminglab

Uploaded by

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

Java Programminglab

Uploaded by

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

Java Programming

• Prerequisites: C Programming, Python Programming


• Course Objectives:
• Gain knowledge about basic Java language syntax and semantics to write Java
programs and use concepts such as variables, conditional and iterative
execution methods etc.
• Understand the principles of inheritance, packages and interfaces Understand
fundamentals of programming such as variables, conditional and iterative
execution, methods, etc.
• Understand fundamentals of object-oriented programming in Java, including
defining classes, invoking methods, using class libraries, etc.
• Be aware of the important topics and principles of software development and
ability to write a computer program to solve specified problems.
• Create database connectivity in java and implement GUI applications
Course Outcomes:

1. Implement OOP paradigm using Java.

2. Demonstrate the concepts of polymorphism, and inheritance.

3. Demonstrate the concepts of method overloading, overriding.

4. Implement inheritance and exception handling.

5. Develop GUI application using Swings package.


Procedural Oriented Programming Object-Oriented Programming

Program is divided into small parts called functions. Program is divided into small parts called objects.

Follows a top-down approach. Follows a bottom-up approach.

The function is more important than the data. Data is more important than function.

Examples: C, FORTRAN, Pascal, Basic, Examples: C++, Java, Python, C#,

There is no access specifier in procedural OOP has access specifiers like private, public,
programming. protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.

Does not have any proper way of hiding data so it provides data hiding so it is more secure.
is less secure.
There is no concept of data hiding and inheritance. The concept of data hiding and inheritance is used.
Compiling and Executing a Java
Program
To run a Java program using
your Command Prompt: source code
8 change to the directory of (Hello.java)
your program compile
cd your name/Rollno
byte code
8 compile the program
(Hello.class)
javac Hello.java
execute
8 execute the program
java Hello output
Structure of Java programs
public class <name> {
public static void main(String[] args) {
<statement(s)>;
}
}
8 Every executable Java program consists of a
class...
– that contains a method named main...
• that contains the statements to be executed
Introduction to Java
Characteristics of Java
• Java is simple : easy to learn and use effectively
• Java is object-oriented: Everything in java (constants, variables and methods) are defined inside a class and
accessed through objects.
• Platform independent: write once run anywhere / Java code can run on any device or operating system
with a Java Virtual Machine (JVM) installed.
• Java is distributed
• Java is interpreted
• Java is robust
• Java is secure
• Java is architecture-neutral
• Java is portable
• Java’s performance
• Java is multithreaded
• Java is dynamic
Objects
Object
• Physical as well as logical entity whereas class is the logical entity
only.
• An object has three characteristics:
• state: represents data (value) of an object.
• behavior: represents the behavior (functionality) of an object such as deposit,
withdraw etc.
• identity: Object identity is typically implemented via a unique ID. The value of
the ID is not visible to the external user. But, it is used internally by the JVM to
identify each object uniquely.
• Object is an instance of a class. Class is a template or blueprint from
which objects are created. So object is the instance(result) of a class.
Object
Object Definitions:
• Object is :
• an instance of a class.
• a real-world entity.
• a run time entity.
• an entity which has state and behavior.
• Syntax : Classname objectname=new Classname();
• Sample s = new Sample()
• here s is an object for the class Sample new operator is used to create
an object
Class
• A class can be defined as a template/blueprint that describes the behavior/state that
the object of its type supports.
• A class is declared by use of the class keyword.
• General form of a class definition :
class classname {
type instance-variable1;
type instance-variable2; // ... type instance-variableN;
type methodname1(parameter-list) { // body of method }
type methodname2(parameter-list)
{ // body of method } // ... type methodnameN(parameter-list) { // body of method }
}
• The data, or variables, defined within a class are called instance
variables.
• The code is contained within methods.
• Collectively, the methods and variables defined within a class are called
members of the class.
• In most classes, the instance variables are acted upon and accessed by
the methods defined for that class.
• Collection of objects is called class. It is a logical entity.
• A class can also be defined as a blueprint from which you can create an
individual object.
Types of Java Comments
• Comments are ignored by the compiler and serve as documentation
for your code.
• 3 types of comments in java.
• Single Line Comment: used to comment only one line.
• Example: //This is single line comment

• Multi Line Comment: used to comment multiple lines of code.

• Example: /* Let's declare and

print variable in java.*/


Keywords
A keyword is a reserved word that conveys special meaning to the
Java compiler.
Java Identifiers
• Identifiers are the names that identify the elements in a program.
• Names used for classes, variables, and methods are called identifiers.
Rules:
• All identifiers should begin with a letter (A to Z or a to z), currency character
($) or an underscore (_).
• After the first character, identifiers can have any combination of characters.
• A key word cannot be used as an identifier.
• Most importantly, identifiers are case sensitive ("myVar" and "myvar" are
different variables).
• Examples of legal identifiers: age, $salary, _value, __1_value.
• Examples of illegal identifiers: 123abc, -salary.
Types of Operators in Java
• Arithmetic Operators
• Unary Operators
• Assignment Operator
• Relational Operators
• Logical Operators
• Ternary Operator
• Bitwise Operators
• Shift Operators
• An instance of an operator
Arithmetic Operators:
• Used for mathematical calculations.
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulo - remainder after division)
Unary Operators:
Operate on a single operand.
• + (Unary plus)
• - (Unary minus)
• ++ (Increment)
• -- (Decrement)
• ! (Logical complement)
• - , Negates the value.
• + , Indicates a positive value
• ++ , Increments by 1.
• Post-Increment: Uses value first, then increments.
• Pre-Increment: Increments first, then uses value.
• -- , Decrements by 1.
• Post-Decrement: Uses value first, then decrements.
• Pre-Decrement: Decrements first, then uses value.
• ! , Inverts a boolean value.
Assignment Operators:

• Used to assign values to variables.

• = (Simple assignment)

• +=, -=, *=, /=, %= (Compound assignment operators)


Relational (Comparison) Operators:
• Used to compare two values.
• == (Equal to)
• != (Not equal to)
• > (Greater than)
• < (Less than)
• >= (Greater than or equal to)
• <= (Less than or equal to)
Logical Operators:
• Used to combine or modify boolean expressions.
• && (Logical AND)
• || (Logical OR)
• ! (Logical NOT)
Bitwise Operators:
• Perform operations on individual bits of integer types.
• & (Bitwise AND)
• | (Bitwise OR)
• ^ (Bitwise XOR)
• ~ (Bitwise complement)
• << (Signed left shift)
• >> (Signed right shift)
• >>> (Unsigned right shift)
Ternary (Conditional) Operator:
• A shorthand for an if-then-else statement.
• condition ? expression1 : expression2
Type Comparison Operator:
• Used to check the type of an object.
• instanceof

You might also like