Saturday, March 14, 2020

Variables:

Variable is a name given to a memory location in which, we can store some value which can be used in a program.

Variable Declaration: It is process of specifying what type of data to be stored into the memory location.

              Syntax:     datatype <variable_name>  OR datatype <var1>, <var2>, <var3>

Ex: 

                          int age;  (age is a variable name given to a memory location where we can store integer type of data)

                          float marks;

                          char c;

                          boolean status;

 

1. A Java program can contain any number of variables.

2. In java language, the variable must be first declare then use it. We cannot use the variable without declaring it.

Rules in a variable Declaration:

·         A java program can contain any number of variables

·         The declaration of the variable is dynamic that means we can declare a variable anywhere in a program.

·         The variable in java language must be first declare and use. (java is strongly typed language)

·         When a variable is declared, the memory for that variable is allocated depending upon the data type that is specified

·         Once a variable is declared, the memory for that variable will be allocated and the variable will be automatically initialized with the default value of that corresponding data type.

Data Type                    Default Value            Data Type                     Default Value

  byte                ->                  0                           float                                    0.0

  short               ->                  0                           double                                0.0

  int                  ->                   0                           char                                    space

  long               ->                   0                           boolean                               false

 

              The default data type under integer category is “int”. The default data type under floating point category is “double”.

              If a variable is declared and if we do not provide any value, then that variable will be automatically initialized with the default value.

 

Declaring and Initializing a Variable:

              The variable can be initialized with our own values at the time of declaration.

Syntax:

          datatype <variable_name> = <value>;                     (OR)

          datatype <variable_name1> = value1, <variable_name2> = value2  so on……

Ex:    int a = 12;        OR      int a=10, b=20, c=30;

Initialization: The process of providing a value to the variable for the first time is called a initialization.

Assignment: The process of providing a value to a variable second time onwards is called a assignment.

              The value of a variable can be changed during the execution of a program that a variable can be assigned any number of times but variable can be initialized only one time.

              Ex:       int a=10;          // Initializing

                          int a=20;          // Assigning

Note: We can also call a value as a literal.

                          int a = 10;                    // 10 is integral literal

                          float b = 12.2               // 12.2 is floating point literal

                          char c = ‘$’                  // $ is character literal

Method: A method is a block of code that perform some task or an action.

JVM executes a method only when we call or invoke it.

We write a method once and use it many times. We do not require to write code again and again.

A method must be declared within a class.

A method can be called any number of times.

Method Creation Syntax:

              <access specifier> <return type> <method name>(parameters list){

            //Method body

}



Example of Method Creation:

public void addition(int a, int b) {

           System.out.println(a+b);

}

A method contains Two parts. Method Header and Method Body. Method header contains 4 components those are access specifier, return type, method name and parameters.



Related Posts:

  • Manual Testing QuestionsWhat is Smoke Testing?Smoke Testing: This is build acceptance Testing performed after build is released to the Testing. This Testing is done to check the major functionalities are working fine or not.What is Sanity Testing?Sa… Read More
  • Java Program to Insert array2 into array1 at given position Java Program: Take 2 arrays, write a method to - Insert array2 in to array1 at given position//Output: {12, 13,14,5,6,15}package qasign;import java.util.ArrayList;import java.util.Arrays;public class SampleTest { public… Read More
  • Quick Notes on Selenium IQsWhat is method overload and method override?Method Overload:method overload means methods contain with same name and difference in the parametersThis difference may be in the  -> sequence of the parameters -> numbe… Read More
  • Reverse Each Word in a String Input "hello world java"  1) Reverse each word in a string Input: hello world java     //Output: olleh dlrow avaj String str="hello world java"; String[] tokens=str.split(" "); for(int i=0;i<tokens.length;i++) { for(int j=… Read More
  • Write a Java Program to Add Map Elements and Print  package qasignpackage;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;public class SampleTest {    public … Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts