Programming Articles

Page 2326 of 2547

Can we declare a static variable within a method in java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 7K+ Views

A static filed/variable belongs to the class and it will be loaded into the memory along with the class. You can invoke them without creating an object. (using the class name as reference). There is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.Examplepublic class Sample{    static int num = 50;    public void demo(){       System.out.println("Value of num in the demo method "+ Sample.num);    }    public static void main(String ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 8K+ 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 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

In how many ways we can concatenate Strings in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 415 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 1K+ 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

Can we make Array volatile using volatile keyword in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 2K+ Views

The volatile modifier indicates the JVM that the thread accessing a volatile variable should get data always from the memory. i.e. a thread should not cache the volatile variable.Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.Examplepublic class MyRunnable implements Runnable {    private volatile boolean active;    public void run() {       active = true;       while (active) { // line 1         ...

Read More

Where does Array stored in JVM memory in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 5K+ 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

How can we remove a selected row from a JTable in Java?

raja
raja
Updated on 02-Jul-2020 8K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable component can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable can generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces. We can remove a selected row from a JTable using the removeRow() method of the DefaultTableModel class.Syntaxpublic void removeRow(int row)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class RemoveSelectedRowTest extends JFrame { private JTable table; private DefaultTableModel model; private Object[][] data; private String[] ...

Read More

Can we store objects in an array in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 11K+ Views

Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.Element: Each item stored in an array is called an element.Index: Each location of an element in an array has a numerical index, which is used to identify the element.Storing Objects in an arrayYes, since objects are also considered as datatypes (reference) in Java, you can create an array of the type of a particular class ...

Read More

Can you change size of Array in Java once created?

Maruthi Krishna
Maruthi Krishna
Updated on 02-Jul-2020 9K+ Views

An array is a data structure/container/object that stores a fixed-size sequential collection of elements of the same type. The size/length of the array is determined at the time of creation.The position of the elements in the array is called as index or subscript. The first element of the array is stored at the index 0 and, the second element is at the index 1 and so on.Each element in an array is accessed using an expression which contains the name of the array followed by the index of the required element in square brackets.For example, if an array of 6 ...

Read More
Showing 23251–23260 of 25,466 articles
Advertisements