Programming Articles - Page 2541 of 3363

Is main method compulsory in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

To compile a program, you doesn’t really need a main method in your program. But, while execution JVM searches for the main method. In the Java the main method is the entry point Whenever you execute a program in Java JVM searches for the main method and starts executing from it.The main method must be public, static, with return type void, and a String array as argument.public static int main(String[] args){ }You can write a program without defining a main it gets compiled without compilation errors. But when you execute it a run time error is generated saying “Main method ... Read More

How to convert an object array to an integer array in Java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

7K+ Views

You can convert an object array to an integer array in one of the following ways −By copying each element from integer array to object array −Exampleimport java.util.Arrays; public class ObjectArrayToStringArray {    public static void main(String args[]){       Object[] objArray = {21, 58, 69, 33, 65};       int length = objArray.length;       int intArray[] = new int[length];       for(int i=0; i

Explain narrowing with object in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

2K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Type Casting/type conversion −Converting one primitive datatype into another is known as type casting (type conversion) in Java. You can cast the primitive datatypes in two ways namely, Widening and, Narrowing.Narrowing − Converting a higher datatype to a lower datatype is known as narrowing. In this case the casting/conversion is not done automatically, you need to convert explicitly using the cast operator “( )” explicitly. Therefore, it is known ... Read More

Explain widening with objects in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Type Casting/type conversion − Converting one primitive datatype into another is known as type casting (type conversion) in Java. You can cast the primitive datatypes in two ways namely, Widening and, Narrowing.Widening − Converting a lower datatype to a higher datatype is known as widening. In this case the casting/conversion is done automatically therefore, it is known as implicit type casting. In this case both datatypes should be compatible ... Read More

Can we cast reference variables in Java?

Venkata Sai
Updated on 06-Aug-2019 12:49:02

1K+ Views

Java provides various datatypes to store various data values. It provides 7 primitive datatypes (stores single values) namely, boolean, byte, char, short, int, long, float, double and, reference datatypes (arrays and objects).Casting in JavaConverting one primitive data type into another is known as type casting.Exampleimport java.util.Scanner; public class TypeCastingExample {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer value: ");       int i = sc.nextInt();       long num = i;       System.out.println("Value of the given integer: "+num);    } }OutputEnter an integer ... Read More

What is deep copy? Explain with an example in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

7K+ Views

Creating an exact copy of an existing object in the memory is known as cloning.The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it (clones).In order to use this method, you need to make sure that your class implements the Cloneable interface.Example Live Demoimport java.util.Scanner; public class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);     ... Read More

What is shallow copy? Explain with an example in Java.

Venkata Sai
Updated on 30-Jul-2019 22:30:26

1K+ Views

Creating an exact copy of an existing object in the memory is known as cloning.The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it (clones).In order to use this method, you need to make sure that your class implements the Cloneable interface.Example Live Demoimport java.util.Scanner; public class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);     ... Read More

Can a for statement loop infinitely in java?

Venkata Sai
Updated on 30-Jul-2019 22:30:26

3K+ Views

Using loops in programming languages we can execute a set of statements repeatedly. Java provides various loops namely while loop, for loop and the do while loop.The for statement contains an initialization statement, a condition and, an increment or decrement operation.Initializing statement − The initialization determines the starting value of the loop.Condition − The condition in for loop is a statement returning a boolean value. This condition determines the exit value of the loop. It is executed before the statements of the loop.Increment and decrement − Using this the loop will be incremented or decremented to the next value.After initializing ... Read More

How can we limit the number of characters inside a JTextField in Java?

Alshifa Hasnain
Updated on 15-May-2025 19:39:03

5K+ Views

In this article, we will learn how to limit the number of characters inside a JTextField in Java. Whether you're creating a form for phone numbers, zip codes, or usernames, controlling the maximum number of characters is important for data validation. JTextField A JTextField is one of the most important Swing components that allows the user to an input text value in a single line format. We can restrict the number of characters that the user can enter into a JTextField can be achieved by using the PlainDocument class. PlainDocument Class PlainDocument is a subclass that extends the AbstractDocument, a ... Read More

Using Iterations in Python Effectively

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

266 Views

In this article, we will learn about how to implement iterators and their effective implementation in Python 3.x. Or earlier. Let’ s take a look at various methods available in python which implements iterators.Type 1 − Implementation Of While Loop With Known LengthExample Code Live Demogenre = ("Python", "C", "C++", "Java") print("The topic available on Tutorial's Point are:") i = 0 while (i < len(genre)): print (genre[i]) i += 1ExplanationDue to its less compact structure, this method is not favored. Error Handling is also difficult in this case . Large-scale programs or designs doesn’t use ... Read More

Advertisements