Found 4338 Articles for Java 8

Compare two objects of Character type in Java

karthikeya Boyini
Updated on 26-Jun-2020 06:59:37

441 Views

To compare two objects of Character type in Java, use the compareTo() method.Firstly, we have created two Character type.Character one = new Character('m'); Character two = new Character('k');Now, to compare them, we have used the compareTo() method.int res = one.compareTo(two);The following is the example that compares character objects.Example Live Demopublic class Demo {    public static void main(String []args) {       Character one = new Character('m');       Character two = new Character('k');       System.out.println("Character object 1: "+one);       System.out.println("Character object 2: "+two);       int res = one.compareTo(two);       if ... Read More

Different Methods to find Prime Number in Java

Samual Sam
Updated on 26-Jun-2020 07:03:02

2K+ Views

A prime number is a number that is only divisible by one or itself. Some of the prime numbers are 2, 3, 5, 7, 11, 13 etc.Some of the different methods to find a prime number in Java are given as follows −Method 1 - Find if a number is prime without using a functionA program that finds if the given number is prime without using a function is given as follow −Example Live Demopublic class Example {    public static void main(String args[]) {       int num = 11, flag=0;       if(num == 0||num == 1) {          System.out.println( num + " is not a prime number");       } else {          for(int i = 2; i

Permutation and Combination in Java

karthikeya Boyini
Updated on 26-Jun-2020 07:04:26

4K+ Views

Permutation and Combination are a part of Combinatorics. Permutation is the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time or all at a time. Combination is is the different ways of selecting elements if the elements are taken one at a time, some at a time or all at a time.An example of this is given as follows −Permutation = factorial(n) / factorial(n-r); Combination = factorial(n) / (factorial(r) * factorial(n-r)); n = 5 r = 3 Permutation = 60 Combination = 10A program that demonstrates this ... Read More

Stack in Java

Samual Sam
Updated on 26-Jun-2020 07:05:40

1K+ Views

A stack class is provided by the Java collection framework and it implements the Stack data structure. The stack implements LIFO i.e. Last In First Out. This means that the elements pushed last are the ones that are popped first.The following are some of the methods.Sr.NoMethods & Description1boolean empty()Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements.2Object peek( )Returns the element on the top of the stack, but does not remove it.3Object pop( )Returns the element on the top of the stack, removing it in the process.4Object push(Object ... Read More

Java program to remove items from Set

karthikeya Boyini
Updated on 26-Jun-2020 07:09:04

2K+ Views

A set in Java is modelled after the mathematical set and it cannot contain duplicate elements. The set interface contains the methods that are inherited from Collection. The method remove() removes the specified items from the set collection.A program that demonstrates the removal of items from Set using remove() is given as follows −Example Live Demoimport java.util.*; public class Example {    public static void main(String args[]) {       int arr[] = {5, 10, 10, 20, 30, 70, 89, 10, 111, 115};       Set set = new HashSet();       try {         ... Read More

Java Program to convert a Primitive Type Value to a String

Samual Sam
Updated on 26-Jun-2020 06:23:50

2K+ Views

For converting a primitive type value to a string in Java, use the valueOf() method.Let’s say we want to convert the following double primitive to string.double val4 = 8.34D;For that, use the valueOf() method. It converts it into a string.String str4 = String.valueOf(val4);Example Live Demopublic class Demo {    public static void main(String[] args) {       int val1 = 5;       char val2 = 'z';       float val3 = 9.56F;       double val4 = 8.34D;       System.out.println("Integer: "+val1);       System.out.println("Character: "+val2);       System.out.println("Float: "+val3);       ... Read More

Convert Char array to String in Java

karthikeya Boyini
Updated on 26-Jun-2020 06:24:54

587 Views

The valueOf() method is used in Java to convert char array to string.Here is our char array.// char array char[] c = {'p','q','r','s','t','u','v'};To convert it to string, use the valueOf() method.String str = String.valueOf(c);Example Live Demopublic class Demo {    public static void main(String[] args) {       // char array       char[] c = {'p','q','r','s','t','u','v'};       // converting to string       String str = String.valueOf(c);       System.out.println("String: "+str);    } }OutputString: pqrstuv

Convert short to String in Java

Samual Sam
Updated on 26-Jun-2020 06:28:00

4K+ Views

The valueOf() method is used in Java to convert short to string.Let’s say we have the following short value.short val = 20;Converting the above short value to string.String.valueOf(val);Example Live Demopublic class Demo {    public static void main(String[] args) {       short shortVal = 55;       // converting short to String       String str = String.valueOf(shortVal);       System.out.println("String: "+str);    } }OutputString: 55

Java Program to convert float to String

Samual Sam
Updated on 26-Jun-2020 06:30:12

211 Views

The valueOf() method is used in Java to convert float to string.Let’s say we have the following float value.float val = 10.18465F;Converting the above float value to string.String.valueOf(val);Example Live Demopublic class Demo {    public static void main(String[] args) {       float val = 98.18465F;       // converting float to String       String str = String.valueOf(val);       System.out.println("String: "+str);    } }OutputString: 98.18465

Keywords in Java

Samual Sam
Updated on 26-Jun-2020 07:04:17

661 Views

Keywords in Java are reserved words that represent predefined actions, internal processes etc. Because of this, keywords cannot be used as names of variables, functions, objects etc.The main difference between keywords and identifiers is that keywords are reserved words that represent predefined actions while identifiers are the names of variables, functions, objects etc.Some of the keywords in the Java are given as follows −abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodoubleelseenumextendsfinalfinallyfloatforgotoifimplementsimportinstanceofintinterfacelongnativenewpackageprivateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttryvoidvolatilewhileA program that demonstrates keywords is given as follows −Example Live Demopublic class Example { public static void main(String[] args) { int i = 5; char c = 'A'; System.out.println("i = " + i); System.out.println("c = " + ... Read More

Advertisements