Found 7442 Articles for Java

Stack in Java

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

2K+ 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 31-Jul-2024 17:51:18

4K+ 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 − Problem Statement Given a Set, write a Java program to remove the elements from the Set − Input [115, 20, 5, 70, 89, 10, 30, 111] Output [115, 20, 5, 70, 10, 30, 111] ... Read More

Java Program to convert a Primitive Type Value to a String

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

3K+ 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

829 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

5K+ 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

300 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

1K+ 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

Deque in Java

Samual Sam
Updated on 26-Jun-2020 06:18:01

925 Views

The dequeue is a double ended queue and data elements can be added or removed from either end. The dequeue in Java is implemented using the java.util.Deque interface which is a subtype of the java.util.Queue interface.A program that demonstrates some of the methods of a dequeue is given as follows −Example Live Demoimport java.util.*; public class Example {    public static void main(String[] args) {       Deque d = new LinkedList();       d.add("5");       d.addFirst("1");       d.addLast("9");       d.push("7");       d.offer("8");       d.offerFirst("6");       d.offerLast("2"); ... Read More

Time Functions in Java

karthikeya Boyini
Updated on 26-Jun-2020 06:19:02

820 Views

Java provides the Date class available in java.util package, this class encapsulates the current date and time. The time functions can be accessed from the java.util.Date class. This represents an instance of time with millisecond precision.One of the time function in Java is the getTime() function. It returns the number of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. A program that demonstrates this is given as follows −Example Live Demoimport java.util.*; public class Example {    public static void main(String[] args) {       Date d = new Date(95, 7, 15);       long num = ... Read More

Multiplication of two Matrices using Java

Samual Sam
Updated on 26-Jun-2020 06:22:34

7K+ Views

Matrix multiplication leads to a new matrix by multiplying 2 matrices. But this is only possible if the columns of the first matrix are equal to the rows of the second matrix. An example of matrix multiplication with square matrices is given as follows.Example Live Demopublic class Example {    public static void main(String args[]) {       int n = 3;       int[][] a = { {5, 2, 3}, {2, 6, 3}, {6, 9, 1} };       int[][] b = { {2, 7, 5}, {1, 4, 3}, {1, 2, 1} };       int[][] ... Read More

Advertisements