Found 4338 Articles for Java 8

Can we use "this" keyword in a static method in java?

Maruthi Krishna
Updated on 05-Aug-2019 12:17:53

5K+ Views

The static methods belong to the class and they will be loaded into the memory along with the class. You can invoke them without creating an object. (using the class name as reference).Examplepublic class Sample{    static int num = 50;    public static void demo(){       System.out.println("Contents of the static method");    }    public static void main(String args[]){       Sample.demo();    } }OutputContents of the static methodThe "this" keyword is used as a reference to an instance. Since the static methods doesn’t have (belong to) any instance you cannot use the "this" reference within ... Read More

How to fix "Exception in thread main" in java?

Maruthi Krishna
Updated on 02-Jul-2020 13:41:17

16K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Exampleimport java.util.Scanner; public class ExceptionExample {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number: ");       int a = sc.nextInt();       System.out.println("Enter second number: ");       int b = sc.nextInt();       int c = a/b;       System.out.println("The result is: "+c);   ... Read More

What is the syntax for passing Scanner object as a parameter in a method using java?

Maruthi Krishna
Updated on 02-Jul-2020 13:43:08

4K+ Views

Until Java 1.5 to read data from the user programmers used to depend on the character stream classes and byte stream classes.From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions.By default, whitespace is considered as the delimiter (to break the data into tokens).To read various datatypes from the source using the nextXXX() methods provided by this class namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), next().Passing Scanner object as a ... Read More

How to resolve javac is not recognized as an internal or external command in java?

Maruthi Krishna
Updated on 05-Aug-2019 11:52:10

2K+ Views

When you compile a program if you see this error it indicates that either you have not installed Java in your system properly or, you haven’t set the Path variable.The Path variable − The path environment variable is used to specify the set of directories which contains executional programs.When you try to execute a program from command line, the operating system searches for the specified program in the current directly, if available, executes it.In case the programs are not available in the current directory, operating system verifies in the set of directories specified in the ‘PATH’ environment variable.Setting Up the ... Read More

How do we set the Java environment variable in cmd.exe?

Maruthi Krishna
Updated on 02-Jul-2020 13:28:13

699 Views

When you install Java in your system first of all you need to set the environment variables which are path and class path.PATH− The path environment variable is used to specify the set of directories which contains executional programs.When you try to execute a program from command line, the operating system searches for the specified program in the current directly, if available, executes it.In case the programs are not available in the current directory, operating system verifies in the set of directories specified in the ‘PATH’ environment variable.You need to set path for compiler (javac.exe) and JVM(java.exe), which exists in ... Read More

In how many ways we can concatenate Strings in Java?

Maruthi Krishna
Updated on 02-Jul-2020 13:30:09

262 Views

Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.You can create a String either by using the new keyword (like any other object) or, by assigning value to the literal (like any other primitive datatype).String stringObject = new String("Hello how are you"); String stringLiteral = "Welcome to Tutorialspoint";Concatenating StringsYou can concatenate Strings in Java in the following ways −Using the "+" operator: Java Provides a concatenation operator using this, you can directly add two String literalsExampleimport java.util.Scanner; public class StringExample {    public ... Read More

Why should you be careful about String concatenation (+) operator in loops using Java?

Maruthi Krishna
Updated on 02-Jul-2020 13:32:40

4K+ Views

Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.You can create a String either by using the new keyword (like any other object) or, by assigning value to the literal (like any other primitive datatype).Public class Sample{    Public static void main(String args[]){       String str1 = "Hello";       String str2 = "how are you";    } }Strings are immutable in Java i.e. once you create a String literal it cannot be modified.StorageSince all the String values we define ... Read More

How and where does String literals in Java stored in the memory?

Maruthi Krishna
Updated on 02-Jul-2020 13:34:36

7K+ Views

Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.You can create a String either by using the new keyword (like any other object) or, by assigning value to the literal (like any other primitive datatype).Examplepublic class StringDemo {    public static void main(String args[]) {       String stringObject = new String("Hello how are you");       System.out.println(stringObject);       String stringLiteral = "Welcome to Tutorialspoint";       System.out.println(stringLiteral);    } }OutputHello how are you Welcome to TutorialspointStorage of ... Read More

Where does Array stored in JVM memory in Java?

Maruthi Krishna
Updated on 02-Jul-2020 13:14:30

4K+ Views

Array is a container which can hold a fix number of entities, which are of the of the same type. Each entity of an array is known as element and, the position of each element is indicated by an integer (starting from 0) value known as index.Exampleimport java.util.Arrays; public class ArrayExample {    public static void main(String args[]) {       Number integerArray[] = new Integer[3];       integerArray[0] = 25;       integerArray[1] = 32;       integerArray[2] = 56;       System.out.println(Arrays.toString(integerArray));    } }Output[25, 32, 56]Arrays are two types −Single dimensional arrays: Normal ... Read More

Java program to verify whether a given element exists in an array.

Maruthi Krishna
Updated on 05-Aug-2019 11:26:59

865 Views

You can find whether a particular exists in a given array using any of the search algorithms. Here, we will see examples for linear search and binary search.Linear searchIterate through the array.Compare each element with the required element.import java.util.Scanner; public class ArraySearch {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created: ");       int size = sc.nextInt();       int[] myArray = new int[size];       System.out.println("Enter the elements of the array: ");       for(int i=0; i

Advertisements