Variables and Data Types

1) What are identifiers in java?

Identifiers are names in java program. Identifiers can be class name, method name or variable name. Rules for defining identifiers in java:

  1. Identifiers must start with letter,Underscore or dollar($) sign.
  2. Identifiers can’t start with numbers .
  3. There is no limit on number of characters in identifier but not recommended to have more than 15 characters.
  4. Java identifiers are case sensitive.
  5. First letter can be alphabet ,or underscore and dollar sign. From second letter we can have numbers.
  6. We shouldn’t use reserve words for identifiers in java.

2) What are the two types of variables in Java?

The two types of variables in Java are primitive variables and reference variables.

3) What is the difference between a primitive variable and a reference variable?

Primitive variables store the actual value, whereas reference variables store the memory address of an object.

4) What are the eight primitive data types in Java?

The eight primitive data types in Java are byte, short, int, long, float, double, char, and boolean.

5) How do you declare a variable in Java?

To declare a variable in Java, you use the keyword β€œvar” or the specific data type followed by the variable name. Example: int myVariable;

6) What is the difference between a local variable and an instance variable in Java?

A local variable is defined within a method or constructor and can only be accessed within that block of code. An instance variable is defined within a class and can be accessed by all methods and constructors of that class.

7) What is the default value of an instance variable in Java?

The default value of an instance variable in Java is null.

8) How do you assign a value to a variable in Java?

To assign a value to a variable in Java, use the assignment operator (=). Example: myVariable = 5;

9) How do you declare and initialize a variable in Java in one line of code?

To declare and initialize a variable in one line of code in Java, you can use the assignment operator (=) after the variable declaration. Example: int myVariable = 5;

10) How do you convert a string to an integer in Java?

To convert a string to an integer in Java, you can use the Integer.parseInt() method. Example: int myInt = Integer.parseInt(β€œ5”);

11) How do you convert a float to a string in Java?

To convert a float to a string in Java, you can use the Float.toString() method. Example: String myString = Float.toString(5.5f);

12) is the difference between a static variable and a non-static variable in Java?

A static variable, also known as a class variable, is associated with a class and is shared by all instances of that class. A non-static variable, also known as an instance variable, is associated with an instance of a class and is unique to each object.

13) What is the difference between a final variable and a constant in Java?

A final variable is a variable that can be assigned a value once and cannot be changed afterwards. A constant is a variable that is declared as final and also has a value that is known at compile-time, and it is also a convention to write the name of constant variables in uppercase letters.

14) How do you compare two strings for equality in Java?

To compare two strings for equality in Java, you can use the .equals() method. Example: if (string1.equals(string2)).

15) How do you concatenate two strings in Java?

To concatenate two strings in Java, you can use the + operator or the .concat() method. Example: string1 + string2 or string1.concat(string2).

16) How do you find the length of a string in Java?

To find the length of a string in Java, you can use the .length() method. Example: int length = myString.length();

17) What is the difference between an int and an Integer in Java?

An int is a primitive data type, while an Integer is an object wrapper class for the int primitive type. An int is a value, while an Integer is an object, this difference can be important in certain situations.

18) How do you declare and initialize an array in Java?

To declare an array in Java, you use the data type followed by square brackets [] followed by the array name. Example: int[] myArray;. To initialize an array, you can use the new keyword followed by the data type and size of the array. Example: myArray = new int[5];

19) How do you access elements in an array in Java?

To access elements in an array in Java, you use the array name followed by the index of the element in square brackets. Example: myArray[0] would access the first element of the array.

20) How can you find the size of an array in Java?

To find the size of an array in Java, you can use the length property of the array. Example: int size = myArray.length;.

Questions? : Reach Out