Found 9326 Articles for Object Oriented Programming

Boolean Type in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:04:35

12K+ Views

To display Boolean type, firstly take two variables and declare them as boolean.boolean val1, val2;Then one by one assign values to both of them, one of them is shown below −val1 = true;Now, use if statement to check and display the Boolean true value.if(val1) System.out.println("This is true and will get displayed!");Let us now see the complete example to work with Boolean Type in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       boolean val1, val2;       System.out.println("Boolean Type in Java");       val1 = true;       if(val1)     ... Read More

Overflow of DataTypes in Java

Samual Sam
Updated on 26-Jun-2020 10:05:01

832 Views

Overflow occurs when the given value is more than the maximum prescribed size of a data type. The overflow condition can result to an error or now the implementation of the programming language handles it on its own.To display overflow of datatypes, I have taken an example of float datatype. Float data type is a single-precision 32-bit IEEE 754 floating point.The range of a float datatype is −approximately ±3.40282347E+38FThe following program display overflow of datatypes in Java.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Displaying Overflow... ");       float val1 = ... Read More

Display the minimum and maximum value of primitive data types in Java

karthikeya Boyini
Updated on 26-Jun-2020 10:05:50

3K+ Views

Every data type in Java has a minimum as well as maximum range, for example, for Float.Min = 1.4E-45 Max = 3.4028235E38Let’s say for Float, if the value extends the maximum range displayed above, it leads to Overflow.However, if the value is less than the minimum range displayed above, it leads to Underflow.The following is the Java Program to display the minimum and maximum value of primitive data types.Example Live Demopublic class Demo {    public static void main(String[] args) {       System.out.println("Integer Datatype values...");       System.out.println("Min = " + Integer.MIN_VALUE);       System.out.println("Max = " ... Read More

Default value of primitive data types in Java

Samual Sam
Updated on 26-Jun-2020 10:06:32

12K+ Views

Primitive datatypes are predefined by the language and named by a keyword in Java. Here is an example to display the default value of primitive data types.Example Live Demopublic class Demo {    static boolean val1;    static double val2;    static float val3;    static int val4;    static long val5;    static String val6;    public static void main(String[] args) {       System.out.println("Default values.....");       System.out.println("Val1 = " + val1);       System.out.println("Val2 = " + val2);       System.out.println("Val3 = " + val3);       System.out.println("Val4 = " + val4);   ... Read More

How Java objects are stored in memory?

Arjun Thakur
Updated on 26-Jun-2020 07:37:24

4K+ Views

A stack and a heap are used for memory allocation in Java. However, the stack is used for primitive data types, temporary variables, object addresses etc. The heap is used for storing objects in memory.Stacks and heaps in Java are explained in more detail as follows −Stack in JavaStacks are used to store temporary variables, primitive data types etc. A block in the stack exists for a variable only as long as the variable exists. After that, the block data is erased and it can be used for storing another variable.

Find max and min values in array of primitives using Java

Ankith Reddy
Updated on 26-Jun-2020 07:37:59

539 Views

This example shows how to search the minimum and maximum element in an array by using Collection.max() and Collection.min() methods of Collection class.Example Live Demoimport java.util.Arrays; import java.util.Collections; public class Main {    public static void main(String[] args) {       Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};       int min = (int) Collections.min(Arrays.asList(numbers));       int max = (int) Collections.max(Arrays.asList(numbers));       System.out.println("Min number: " + min);       System.out.println("Max number: " + max);    } }OutputThe above code sample will produce the following result.Min number: 1 Max number: 9Another ... Read More

Find frequency of each word in a string in Java

Chandu yadav
Updated on 26-Jun-2020 07:38:47

1K+ Views

In order to get frequency of each in a string in Java we will take help of hash map collection of Java.First convert string to a character array so that it become easy to access each character of string.Now compare for each character that whether it is present in hash map or not in case it is not present than simply add it to hash map as key and assign one as its value.And if character is present than find its value which is count of occurrence of this character in the string (initial we put as 1 when it ... Read More

Fibonacci of large number in java

George John
Updated on 26-Jun-2020 07:39:23

511 Views

Fibonacci numbers of Fibonacci series grows exponentially and can be very large for large numbers like 500 or 1000. To handle such number, long data type is not sufficient. BigInteger can handle large number easily. BigInteger is useful in scenarios where calculations results in data which is out of limit for available primitive data types. See the example below of getting Fibonacci number of 100 and 1000.Example Live Demoimport java.math.BigInteger; public class Tester {    public static void main(String args[]) {       System.out.println("Fibonacci of 100: ");       System.out.println(fibonacci(100));       System.out.println("Fibonacci of 1000: ");     ... Read More

Does JVM creates object of Main class in Java?

Ankith Reddy
Updated on 26-Jun-2020 07:40:55

384 Views

As we know that Java needs main() method to be static in the public class to make it executable. The prime reason for this requirement is to make JVM enable to call the main() method without creating an object. That simply means that JVM does not create the object of the Main class which contains the main() method. To justify the same, we can make the Main class containing the main method as abstract and program still runs.Following example showcases the same. Here we have made the main class abstract.Exampleabstract public class Tester {    public static void main(String args[]) ... Read More

Difference between x++ and x= x+1 in Java programming

Arjun Thakur
Updated on 26-Jun-2020 07:41:18

530 Views

x++ automatically handles the type casting where as x= x + 1 needs typecasting in case of x is not an int variable. See the example below.Example Live Demopublic class Tester {    public static void main(String args[]) {       byte b = 2;       //Type casting is required       //as 1 is int and b is byte variable       b = (byte) (b + 1);       System.out.println(b);       byte b1 = 2;       //Implcit type casting by the compiler       b1++;       System.out.println(b1);    } }Output3 3

Advertisements