Found 9326 Articles for Object Oriented Programming

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

570 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

208 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

649 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

653 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

676 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

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

Sort the words in lexicographical order in Java

karthikeya Boyini
Updated on 25-Jun-2020 14:03:52

2K+ Views

The words are sorted in lexicographical order or dictionary order. This means that the words are alphabetically ordered based on their component alphabets. An example of this is given as follows.The original order of the words is Tom Anne Sally John The lexicographical order of the words is Anne John Sally TomA program that demonstrates this is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) {       String[] words = { "Peach", "Orange", "Mango", "Cherry", "Apple" };       int n = 5;       System.out.println("The original order of the words ... Read More

Advertisements