Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views15 pages

Java Programming Concepts Explained

The document contains multiple Java programming examples covering various concepts such as command line arguments, OOP principles, inheritance, polymorphism, exception handling, multithreading, file handling, and more. Each example includes a code snippet and its corresponding output, demonstrating the functionality of the Java features discussed. The topics range from basic syntax to advanced concepts like constructor overloading and the use of the super keyword.

Uploaded by

kratgya2006
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)
43 views15 pages

Java Programming Concepts Explained

The document contains multiple Java programming examples covering various concepts such as command line arguments, OOP principles, inheritance, polymorphism, exception handling, multithreading, file handling, and more. Each example includes a code snippet and its corresponding output, demonstrating the functionality of the Java features discussed. The topics range from basic syntax to advanced concepts like constructor overloading and the use of the super keyword.

Uploaded by

kratgya2006
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

1.

Java Program using command line arguments

// [Link]

public class CommandLineExample {

public static void main(String[] args) {

[Link]("Number of arguments: " + [Link]);

for (int i = 0; i < [Link]; i++) {

[Link]("Argument " + i + ": " + args[i]);

Output:

Number of arguments: 2

Argument 0: Hello

Argument 1: World
[Link] OOP Concepts – Class , Object ,
Method

// [Link]

class Car {

String color = "Red";

void display() {

[Link]("Color of the car is: " + color);

public static void main(String[] args) {

Car myCar = new Car(); // object creation

[Link](); // calling method

Output:

Color of the car is: Red


[Link] and Polymorphism

class Animal {

void sound() {

[Link]("Animal makes a sound");

class Dog extends Animal {

void sound() {

[Link]("Dog barks");

public static void main(String[] args) {

Animal a = new Dog(); // Polymorphism

Output:

Dog barks
[Link] Handling and Multithreading

class MyThread extends Thread {

public void run() {

[Link]("Thread is running");

public static void main(String[] args) {

MyThread t = new MyThread();

[Link]();

Output:

Thread is running
[Link] Packages Example

package mypack;

public class PackageExample {

public void show() {

[Link]("This is from my package");

public static void main(String[] args) {

PackageExample obj = new PackageExample();

[Link]();

Output:

This is from my package


[Link] I/O Example

import [Link].*;

public class IOExample {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new


InputStreamReader([Link]));

[Link]("Enter name: ");

String name = [Link]();

[Link]("Hello " + name);

Output:

Enter name: Krati

Hello Krati
[Link] Implementation

interface Animal {

void sound();

class Cat implements Animal {

public void sound() {

[Link]("Meow");

public static void main(String[] args) {

Cat c = new Cat();

[Link]();

Output:

Meow
[Link] Handling Example

import [Link];

import [Link];

public class FileWrite {

public static void main(String[] args) {

try {

FileWriter writer = new FileWriter("[Link]");

[Link]("Writing to file in Java");

[Link]();

[Link]("File written successfully");

} catch (IOException e) {

[Link]("Error occurred.");

Output:
File written successfully
[Link] Handling

public class ExceptionExample {

public static void main(String[] args) {

try {

int a = 10 / 0;

} catch (ArithmeticException e) {

[Link]("Cannot divide by zero");

Output:
Cannot divide by zero
[Link] Example

public class ArrayExample {

public static void main(String[] args) {

int[] arr = {1, 2, 3};

for(int i : arr) {

[Link](i);

Output:

3
[Link] Operations

public class StringExample {

public static void main(String[] args) {

String s = "Hello";

[Link]("Length: " + [Link]());

[Link]("Uppercase: " + [Link]

());

Output:

Length: 5

Uppercase: HELLO
[Link] Overloading

class Car {

Car() {

[Link]("Default Constructor");

Car(String name) {

[Link]("Car name: " + name);

public static void main(String[] args) {

new Car();

new Car("BMW");

Output:

Default Constructor

Car name: BMW


[Link] Keyword Usage

class Counter {

static int count = 0;

Counter() {

count++;

[Link](count);

public static void main(String[] args) {

new Counter();

new Counter();

new Counter();

Output:

3
14. Super Keyword Demo

class Base {

void show() {

[Link]("Base class");

classDerivedextendsBase{ void show() {

[Link](); [Link]("Derivedclass");

public class SuperKeyword {

publicstaticvoidmain(String[]args){ Derived d = new Derived();

[Link]();

Output:
Base class

Derived class
[Link] Try-Catch Block

public class NestedTryCatch {

publicstaticvoidmain(String[]args){ try {

try {

int a = 30 / 0;

} catch (ArithmeticException e)
{ [Link]("ArithmeticException");

int[]arr=newint[5]; arr[7] = 3;

} catch (ArrayIndexOutOfBoundsException e)
{ [Link]("Arrayindexoutofbound");

Output:
Arithmetic Exception

Arrayindexoutofbound

You might also like