Found 9321 Articles for Object Oriented Programming

What is the difference between class variables and instance variables in Java?

Johar Ali
Updated on 12-Sep-2023 02:05:05

34K+ Views

Following are the notable differences between Class (static) and instance variables. Instance variables Static (class) variables Instance variables are declared in a class, but outside a method, constructor or any block. Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block. Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Static variables are created when the program starts and destroyed when the program stops. ... Read More

Is String a primitive data type or an object in Java?

Amit Sharma
Updated on 30-Jul-2019 22:30:20

953 Views

String is not a primitive data type. Java.lang package provides the String class therefore, it is an object type. You can create a string variable directly like any other variables as −String s = "myString";(or)By instantiating the string class using the new keyword as −String s = new String("myString");Example Live Demoimport java.util.Scanner; public class StringExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter a sting value:"); String str = sc.nextLine(); System.out.println(str.getClass()); } }OutputEnter a sting value: hello class java.lang.String

What are tokens in Java?

Johar Ali
Updated on 30-Jul-2019 22:30:20

2K+ Views

Java tokens are smallest elements of a program which are identified by the compiler. Tokens in java include identifiers, keywords, literals, operators and, separators.

What are Boolean literals in Java?

Amit Sharma
Updated on 30-Jul-2019 22:30:20

1K+ Views

Boolean literals represent only two values true or false. And in Java the value of 1 is assumed as true and the value of 0 is assumed as false. Example Live Demo public class Test{ public static void main(String args[]) throws Exception{ boolean bool1 = true; boolean bool2 = false; boolean bool = (25==(100/4)); System.out.println(bool1); System.out.println(bool2); System.out.println(bool); } } Output true false true

What is the difference between character literals and string literals in Java?

Ali
Ali
Updated on 30-Jul-2019 22:30:20

680 Views

Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like , \b etc. Whereas, the String literal represents objects of String class. Example Live Demo public class LiteralsExample { public static void main(String args[]){ char ch = 'H'; String str = "Hello"; System.out.println("Value of character: "+ch); System.out.println("Value of string: "+str); } } Output Value of character: H Value of string: Hello

What is the difference between integer and floating point literals in Java?

Rahul Sharma
Updated on 30-Jul-2019 22:30:20

629 Views

Integer literals represent fixed integer values like 900, 12, 400, -222 etc. (with in the integer range). Whereas, floating point literals represents fractional values i.e. numbers with decimal values like 25.53, 45.66, 58.66 etc. while writing these literals we should use the notation f or F as 25.53. Example Live Demo public class StringExample { public static void main(String args[]){ int num1 = 100; float num2 = 30.0f; System.out.println("Value of integer:"+num1); System.out.println("Value of integer:"+num2); } } Output Value of integer:100 Value of integer:30.0

What are literals in Java?

Johar Ali
Updated on 30-Jul-2019 22:30:20

2K+ Views

A literal is a source code representation of a fixed value. They are represented directly in the code without any computation. Literals can be assigned to any primitive type variable. Example byte a = 68; char a = 'A' byte, int, long, and short can be expressed in decimal(base 10), hexadecimal(base 16) or octal(base 8) number systems as well. Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For example − int decimal = 100; int octal = 0144; int hexa = 0x64; String ... Read More

What is the difference between data types and literals in Java?

Amit Sharma
Updated on 30-Jul-2019 22:30:20

2K+ Views

Data types are those which specify the type of data represented by the variable and literal is the that is stored in to the variable. A literal is a source code representation of a fixed value. They are represented directly in the code without any computation. Literals can be assigned to any primitive type variable. Example byte a = 68; char a = 'A'

What is the difference between Java references and pointers in other languages?

Ali
Ali
Updated on 30-Jul-2019 22:30:20

240 Views

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Following are the reference types in Java. class types − This reference type points to an object of a class. array types − This reference type points to an array. interface types − This reference type points to an object of a class which implements an interface. Once we create a variable of these types (i.e. when we create an array or object, class or interface). These variables only store the address of these values. Default ... Read More

What are reference data types in Java?

Rahul Sharma
Updated on 30-Jul-2019 22:30:20

8K+ Views

Reference datatypes in java are those which contains reference/address of dynamically created objects. These are not predefined like primitive data types. Following are the reference types in Java. class types − This reference type points to an object of a class. array types − This reference type points to an array. interface types − This reference type points to an object of a class which implements an interface. Once we create a variable of these types (i.e. when we create an array or object, class or interface). These variables only store the address of these values. Default ... Read More

Advertisements