OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Java Statements and Command Line Arguments

 

Java Statements 

 statements are instructions that tell the program what to do. They are the building blocks of any Java program and are typically terminated with a semicolon (;). Here is a detailed overview of Java statements:

Types of Java Statements

1. Declaration Statements

Used to declare variables and assign types.

int a;
String name;

2. Expression Statements

These include assignments, method calls, increment/decrement operations, and object creation.


a = 10; // assignment System.out.println(a); // method call a++; // increment new Scanner(System.in); // object creation

3. Control Flow Statements

Used to control the flow of execution.

a. Conditional Statements

  • if, if-else, if-else-if, switch

if (a > 0) {
System.out.println("Positive"); } else { System.out.println("Negative"); }

switch (a) { case 1: System.out.println("One"); break; default: System.out.println("Other"); }

b. Looping Statements

  • for, while, do-while

for (int i = 0; i < 5; i++) {
System.out.println(i); } int j = 0; while (j < 5) { System.out.println(j); j++; } int k = 0; do { System.out.println(k); k++; } while (k < 5);

c. Branching Statements

  • break, continue, return

for (int i = 0; i < 10; i++) {
if (i == 5) break; // exits loop if (i % 2 == 0) continue; // skips even numbers System.out.println(i); } return; // exits from a method

Compound Statements / Block Statements

  • A group of statements enclosed in { }. These define a block.


{ int x = 10; System.out.println(x); }

Empty Statement

A statement that does nothing — just a semicolon (;).

;

Labeled Statements

Used with loops and branching (break, continue).

outer:
for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (j == 2) break outer; System.out.println(i + ", " + j); } }

Example Java Program with Various Statements


public class Demo { public static void main(String[] args) { int a = 10; // declaration + expression if (a > 0) { // control flow System.out.println("Positive number"); } for (int i = 0; i < 3; i++) { // loop System.out.println(i); } // method call printMessage(); } static void printMessage() { System.out.println("Hello from method"); } }






==============================================================================================

 Command Line Arguments

Sometimes you will want to pass information into a program when you run it. This is accomplished by passing command-line arguments to main( ). A command-line argument is the information that directly follows the program’s name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy—they are stored as strings in a String array passed to the args parameter of main( ). The first command-line argument is stored at args[0], the second at args[1], and so on.


Syntax of main() Method

public static void main(String[] args)
  • args is an array of String objects.

  • These are the values provided at runtime via command line.

  • Java automatically populates the args array with these values.


How to Pass Command Line Arguments

Example Command (using terminal or command prompt):


java MyProgram Hello 123

In the Java Program:


public class MyProgram { public static void main(String[] args) { System.out.println("Number of arguments: " + args.length); for (int i = 0; i < args.length; i++) { System.out.println("Argument " + i + ": " + args[i]); } } }

Output:

Number of arguments: 2
Argument 0: Hello Argument 1: 123

Converting Arguments to Other Data Types

Since all command line arguments are Strings, you must convert them if needed:

int number = Integer.parseInt(args[0]);
double value = Double.parseDouble(args[1]);

Example Program: Add Two Numbers from Command Line

public class AddNumbers {
public static void main(String[] args) { if (args.length < 2) { System.out.println("Please provide two numbers"); return; } int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); int sum = num1 + num2; System.out.println("Sum: " + sum); } }

Run in Terminal:

java AddNumbers 10 20

Output:

Sum: 30

 Another example:

For example, the following program displays all of the command-line arguments that it is called with:

// Display all command-line arguments.

class CommandLine {

            public static void main(String args[])

 {

 for(int i=0; i<args.length; i++)

System.out.println("args[" + i + "]: " +args[i]);

}

}

Try executing this program, as shown here:

java CommandLine this is a test 100 -1

When you do, you will see the following output:

args[0]: this args[1]: is args[2]: a args[3]: test args[4]: 100

args[5]: -1

NOTE: All command line arguments are passed as strings. You must convert numeric values to their internal forms manually.


Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC