Java Articles

Page 408 of 450

How can I append text to JTextArea in Java?

George John
George John
Updated on 30-Jul-2019 3K+ Views

To append text to JTextArea, use the component’s append() method. Let’sa say the following is our text in the JTextArea -JTextArea textArea = new JTextArea("The text added here is just for demo. "+ "This demonstrates the usage of JTextArea in Java. ");Now, append text to the same textArea -textArea.append("In this example we have deleted some text from the beginning."+ "We have also appended some text.");The following is an example to append text to JTextArea -Examplepackage my; import java.awt.GridLayout; import javax.swing.*; public class SwingDemo {    SwingDemo() {       JFrame frame = new JFrame("Demo");       JTextArea textArea ...

Read More

How to iterate the values in an enum in Java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 17K+ Views

Enumeration (enum) in Java is a datatype which stores a set of constant values. You can use enumerations to store fixed values such as days in a week, months in a year etc.enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }You can iterate the contents of an enumeration using for loop, using forEach loop and, using java.util.stream.Using for loopYou can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.ExampleFollowing java program iterates ...

Read More

What happens when you add a double value to a String in java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 2K+ Views

The “+” operator with a String acts as a concatenation operator.Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object.In-fact adding a double value to String is the easiest way to convert a double value to Strings.Exampleimport java.util.Scanner;    public class StringsExample {       public static void main(String args[]){          Scanner sc = new Scanner(System.in);          System.out.println("Enter a double value: ");          double d = sc.nextDouble();          System.out.println("Enter a String value: ");       ...

Read More

What are the rules to be followed while using varargs in java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 341 Views

Since JSE1.5 you can pass a variable number of values as argument to a method. These arguments are known as var args and they are represented by three dots (…)Syntaxpublic myMethod(int ... a) { // method body }Rules to follow while using varargs in JavaWe can have only one variable argument per method. If you try to use more than one variable arguments a compile time error is generated.ExampleIn the following Java example we are trying to accepts two varargs from the method sample().public class VarargsExample{ void demoMethod(int... ages), String... names) { ...

Read More

How to implement constants in java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 2K+ Views

A constant variable is the one whose value is fixed and only one copy of it exists in the program. Once you declare a constant variable and assign value to it, you cannot change its value again throughout the program.You can create a constant in c language using the constant keyword (one way to create it) as −const int intererConstant = 100; or, const float floatConstant = 16.254; ….. etcConstants in javaUnlike in C language constants are not supported in Java(directly). But, you can still create a constant by declaring a variable static and final.Static − Once you declare a ...

Read More

Can we use Switch statement with Strings in java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 11K+ Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Syntaxswitch(expression) {    case value :       // Statements       break;    case value :       // Statements       break;       // You can have any number of case statements.       default :     // Statements }Strings in switchYes, we can use a switch statement with Strings in Java. While doing so you need ...

Read More

What are the rules to be followed while making a variable static and final?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 383 Views

Static variables − Static variables are also known as class variables. You can declare a variable static using the keyword. Once you declare a variable static there would only be one copy of it in the class, regardless of how many objects are created from it.public static int num = 39;final − Once you declare a variable final you cannot reassign value to it. When you declare a variable of a class static and final we are making it constant.Rules to be followedInitialization is mandatory − It is not mandatory to initialize the instance variables of a class in java. ...

Read More

Why is operator overloading not supported by java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 13K+ Views

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Operator overloadingOperator overloading is the ability to redefine the functionality of the operators. Programming languages like c++ supports operator overloading.you can redefine or overload most of the built-in operators available in C++. Thus, a programmer can use operators with user-defined types as well.Overloaded operators are functions with special names: the keyword "operator" followed ...

Read More

Can we overload a method based on different return type but same argument type and number, in java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 9K+ Views

When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading.Exampleclass Test{ public int division(int a, int b){ int result = a/b; return result; } public double division (float a, float b){ double result = a/b; ...

Read More

How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters in java?

Venkata Sai
Venkata Sai
Updated on 30-Jul-2019 3K+ Views

In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.ASCII − Stands for American Standards Code for Information Interchange. It is developed by American standards association and is the mostly used coding system. It represents characters using 7 bits and has includes 128 characters: upper and lowercase Latin alphabet, the numbers 0-9, and some extra characters).Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters ...

Read More
Showing 4071–4080 of 4,498 articles
« Prev 1 406 407 408 409 410 450 Next »
Advertisements