Java Variables and Method

Total Post:16

Points:112

 84  View(s)
Ratings:
Rate this:

Java Variables:-

Variables are used for storing the data values.

Various types of variables are:

  •  String
  •  int
  •  float
  • char
  •  Boolean

Declaration of Variables:-

Type variable = value;

Example

String name = “Nitin”;

System.out.println (name);
To Top

A parameter used in Java Program:-

  • class keyword used to declare a class in java.2
  • The public keyword is used to access inside the class outside the class inside the method anywhere.
  •  static is used when we declare any method as static. There is no need to create an object to call the static method.
  •  void means that it does not contain any value.
  •  main represents the starting point of the program.
  •  String args [] is used for line command.
  • System.out.println is used to print the statement.
  •  Private Java private keyword is an access modifier. It is used to indicate that a method or variable may be accessed only in the class in which it is declared

Example’

class Student
{
public static void main (String args [])
{
System.out.println (“I Love you”);
}
}

Compile by-java Student.java

Run by- Student Java

Output;
I Love You.

Java Methods:-

  • A method is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a method.
  • Methods are also known as a function.
  • If we define the code once and use it many times.
  • The method followed by parenthesis ().
  • Java provides some pre-defined method such as System.out.println (). But we can also create own method.

Example;

public class Student
{
static void Mymethod()
{
System.out.println(“I am fine”);
}
public static void main(String args[])
{
Mymethod();
}
}
Output:-
I am fine.


Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.