CS 2nd Lec #03

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS 2nd Lecture No :#03 (OOP)Object Oriented

A simple java program:

public class myprogram{

public static void main(String[] args){


System.out.println("Hello World!");
}

 Class Declaration:

 This line creates a class named myprogram.


 A class is a blueprint for creating objects that contain data (variables)
and actions (methods).
 The public keyword means the class is accessible from any other part
of the program.

 Main Method:

 This declares the main method, which is the entry point of any Java
application.
 When you run the program, the execution starts from this method.
 The public static void part indicates that this method is:
 public: Accessible from anywhere in the program.
 static: Can be called without creating an object of the class.
 void: Doesn't return any value.
CS 2nd Lecture No :#03 (OOP)Object Oriented

 String[] args is a parameter that allows the program to receive


command-line arguments (although not used in this example).

 Output Statement:

 This line prints the text "Hello World!" to the console.


 System.out is a predefined object that represents the standard
output stream (usually the console).
 println() is a method that prints a string followed by a newline
character.
 When you run this program, you'll see the following output:
Hello World!

 Identifiers:
Identifiers are unique names you give to program elements like
variables, functions, classes, and packages.
They follow specific naming conventions:

 Start with a letter, underscore (_), or dollar sign ($).


 Subsequent characters can be letters, underscores, or digits (0-9).
 Case-sensitive (e.g., age is different from Age).
 Reserved keywords (like int, if, else) cannot be used as identifiers.

 Variables:

 Variables act like named storage containers in your program's


memory.
 You declare them with a data type specifying the kind of information
they can hold (e.g., int for integers, String for text).
 Once declared, you can assign a value to a variable using the
assignment operator (=).
CS 2nd Lecture No :#03 (OOP)Object Oriented

Example:

 Assignments:

Assignments use the = operator to store a value in a variable.


The value on the right side of the operator is evaluated first, then stored
in the variable on the left.

 Statements:
Statements are complete instructions that the program can execute.
They perform actions like:

 Declaring variables
 Performing calculations
 Controlling program flow (e.g., using if statements for conditional
execution)
 Interacting with the user (e.g., printing messages or reading input)

 Examples:

 Assignment Expressions:

 Assignment expressions combine an assignment and an expression


on the same line.
 The expression on the right side is evaluated first, and its result is
then assigned to the variable on the left.
CS 2nd Lecture No :#03 (OOP)Object Oriented

 Named Constants:

 Named constants are variables whose values cannot be


changed after initialization.
 They are declared using the final keyword before the data
type.
 This ensures the value remains constant throughout the
program, making your code more reliable and easier to
understand.
 Example:

You might also like