Sie sind auf Seite 1von 6

Program 1:

public class MyClass {


  public static void main(String[] args) {
    System.out.println("Hello World");
 }
}

Public -It is an Access Modifier, which defines who can access this Method. Public means that this Method
will be accessible by any Class (If other Classes can access this Class.).

Static -Static is a keyword which identifies the class related thing. It means the given Method or variable is
not instance related but Class related. It can be accessed without creating the instance of a Class.

Void -Is used to define the Return Type of the Method. It defines what the method can return. Void means
the Method will not return any value.

Main- Main is the name of the Method. This Method name is searched by JVM as a starting point for an
application with a particular signature only.

String args[] / String… args - It is the parameter to the main Method. Argument name could be anything.

Program2 :

public class MyClass {

public static void main(String[] args) {

String name = "John";

System.out.println("Hello " + name);

Program 3:

public class MyClass {

static void myMethod() {

System.out.println("I just got executed!");

public static void main(String[] args) {


myMethod();

Program 4:

public class MyClass {

public static void main(String[] args) {

String firstName = "John ";

String lastName = "Doe";

String fullName = firstName + lastName;

System.out.println(fullName);

Program5:

public class MyClass {

public static void main(String[] args) {

String x = "10";

int y = 20;

String z = x + y;

System.out.println(z);

public class StringDemo {

public static void main(String args[]) {


char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println( helloString );
}
}
public class StringDemo {

public static void main(String args[]) {


String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
System.out.println( "String Length is : " + len );
}
}

import java.util.Arrays;

public class MainClass {


public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 2);
System.out.println("Found 2 @ " + index);
}
private static void printArray(String message, int array[]) {
System.out.println(message + ": [length: " + array.length + "]");

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


if(i != 0) {
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
}

Result
The above code sample will produce the following result.

Sorted array: [length: 10]


-9, -7, -3, -2, 0, 2, 4, 5, 6, 8
Found 2 @ 5

import java.util.Scanner;  // Import the Scanner class

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input


    System.out.println("Username is: " + userName);  // Output user input
 }
}

import java.util.Scanner;

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);

    System.out.println("Enter name, age and salary");

    // String input


    String name = myObj.nextLine();

    // Numerical input


    int age = myObj.nextInt();
    double salary = myObj.nextDouble();

    // Output input by user


    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
    System.out.println("Salary: " + salary);
 }
}

Encapsulation
. To achieve Encapsulation, you must:

 declare class variables/attributes as private (only accessible within the same class)
 provide public setter and getter methods to access and update the value of a private variable

Get and Set


private variables can only be accessed within the same class (an outside class has no access to
it). However, it is possible to access them if we provide public getter and setter methods.

The get method returns the variable value, and the set method sets the value.
Syntax for both is that they start with either get or set, followed by the name of the variable,
with the first letter in upper case:

Example
public class Person {
  private String name; // private = restricted access

  // Getter
  public String getName() {
    return name;
 }

  // Setter
  public void setName(String newName) {
    this.name = newName;
 }
}

The get method returns the value of the variable name.

The set method takes a parameter (newName) and assigns it to the name variable. The this
keyword is used to refer to the current object.

However, as the name variable is declared as private, we cannot access it from outside this
class:

Example
public class MyClass {
  public static void main(String[] args) {
    Person myObj = new Person();
    myObj.name = "John";  // error
    System.out.println(myObj.name); // error 
 }
}

If the variable was declared as public, we would expect the following output:

John

However, as we try to access a private variable, we get an error:

MyClass.java:4: error: name has private access in Person


    myObj.name = "John";
         ^
MyClass.java:5: error: name has private access in Person
    System.out.println(myObj.name);
                  ^
2 errors

Instead, we use the getName() and setName() methods to acccess and update the variable:

Example
public class MyClass {
  public static void main(String[] args) {
    Person myObj = new Person();
    myObj.setName("John"); // Set the value of the name variable to "John"
    System.out.println(myObj.getName());
 }
}

// Outputs "John"

Das könnte Ihnen auch gefallen