Found 4338 Articles for Java 8

What is shallow copy? Explain with an example in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

851 Views

Creating an exact copy of an existing object in the memory is known as cloning.The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it (clones).In order to use this method, you need to make sure that your class implements the Cloneable interface.Example Live Demoimport java.util.Scanner; public class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);     ... Read More

Can a for statement loop infinitely in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

Using loops in programming languages we can execute a set of statements repeatedly. Java provides various loops namely while loop, for loop and the do while loop.The for statement contains an initialization statement, a condition and, an increment or decrement operation.Initializing statement − The initialization determines the starting value of the loop.Condition − The condition in for loop is a statement returning a boolean value. This condition determines the exit value of the loop. It is executed before the statements of the loop.Increment and decrement − Using this the loop will be incremented or decremented to the next value.After initializing ... Read More

What are the restrictions on increment and decrement operators in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

The increment operator increments the value of the operand by 1 and the decrement operator decrements the value of the operand by 1. We use these operators to increment or, decrement the values of the loop after executing the statements on a value.Example Live Demopublic class ForLoopExample {    public static void main(String args[]) {       //Printing the numbers 1 to 10       for(int i = 1; i=1; i--) {          System.out.print(" "+i);       }    } }Output1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 ... Read More

Differentiate between the prefix and postfix forms of the ++ operator in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

Java provides two operators namely ++ and --, to increment and decrement values by 1 respectively.There are two variants of these operators −Pre-increment/decrement − This form, increments/decrements the value first, and then performs the specified operation.ExampleIn the following example, the initial value of the variable i is 5. We are printing the incremented value of it using the pre increment operator.Since we are using the pre increment operator, the value of i is incremented then printed. Live Demopublic class ForLoopExample {    public static void main(String args[]) {       int i = 5;       System.out.println(++i);     ... Read More

Are true and false keywords in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

882 Views

Keywords − Keywords in Java convey a special meaning to the compiler therefore, these cannot be used as identifiers. Java provides a set of 50 keywords.abstractcontinuefornewswitchassertdefaultgotopackagesynchronizedbooleandoifprivatethisbreakdoubleimplementsprotectedthrowbyteelseimportpublicthrowscaseenuminstanceofreturntransientcatchextendsintshorttrycharfinalinterfacestaticvoidclassfinallylongstrictfpvolatileconstfloatnativesuperwhileReserved words − Among the list of key words list mentioned above the key words goto and const are currently not in use. They are reserved words (for the future use).The true false and null − True, false and null represents certain values in Java, they are used as literals. They are not considered as keywords.Still, you cannot use them as identifiers in Java if you try to do so, a compile time error will ... Read More

Elaborate the legal operands of the instance of operator in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

94 Views

The instanceof operator in Java is used to find whether a reference is an instance of a Type i.e. class or an interface.Example Live Demopublic class InstanceOfExample {    public static void main(String args[]) {       String str = "hello";       boolean bool = str instanceof String;       System.out.println(bool);    } }OutputtrueLegal operands of the instanceof operatorThe following are the only legal operands of the instanceof operator −Left operand − It must be a reference representing an object.Right operand − It must be the name of Java class or, interface.Except these two if you use ... Read More

What are the rules to be followed while working with a switch statement in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntaxswitch(expression) {    case value :       // Statements       break;    case value :       // Statements       break;       // You can have any number of case statements.    default :       // Statements }Rules to be followedWhile working with a switch statement keep the following points in mind −We must only use int, ... Read More

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.ASCII − Stands for American Standards Code for Information Interchange. It is developed by American standards association and is the mostly used coding system. It represents characters using 7 bits and has includes 128 characters: upper and lowercase Latin alphabet, the numbers 0-9, and some extra characters).Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters ... Read More

Can we cast a double value to a byte in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Casting in JavaConverting one primitive data type into another is known as type casting. There are two types of casting −Widening− Converting a lower datatype to a higher datatype is known as widening. It is done implicitly.Narrowing− Converting a higher datatype to a lower datatype is known as narrowing. You need to do it explicitly using the cast operator (“( )”).Casting double to byteDouble is a higher datatype compared ... Read More

Can you cast a Byte object to a double value in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

712 Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Casting in JavaConverting one primitive data type into another is known as type casting. There are two types of casting −Widening− Converting a lower datatype to a higher datatype is known as widening. It is done implicitly.Narrowing− Converting a higher datatype to a lower datatype is known as narrowing. You need to do it explicitly using the cast operator (“( )”).For every primitive variable a wrapper class is available, ... Read More

Advertisements