answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

When variables are declared in the body of a method they are know as what?

Local Variables

There are two types of variables based on the location of declaration

1. Instance Variables- Declared inside a class, but outside of any method's body.

2. Local Variables- Declared inside a method's body inside a class.

What is the base class for all swing components?

The JComponent class of the javax.swing package serves as the base class.

Why method overloading is not possible by return type method?

A method can be used, and the return type discarded. For example, for a method that returns, say, an integer, instead of calling it like this:

int x;

x = MyMethod(1, 2);

You can also call it like this:

MyMethod(1, 2);

That is, without using the return value. In such a case, if you were to have different methods with the same parameters but different return values, the compiler wouldn't know which version of the method to use.

What is string and how they are declared?

Strings are user defined data types which can store any type of data assigned to it with in double quotes(" "). They don't have a limit to their storage capacity like other data types,viz.,int byte long.

What happens to child thread when you delete main thread during run-time?

In Java, if the main thread somehow exits then all other threads will stop executing. This generally does not happen, as the main thread will wait for all child threads to terminate before the main thread itself finishes. Interrupting this process is hard to do short of an un-handled exception or a call to System.exit.

If the child thread is also a daemon thread, then the child thread will continue to execute. If the child thread is a normal thread then, the moment System.exit is called, the child thread also terminates

If you call the join() method from the child thread, then the main thread will wait until the child is over before executing the System.exit

How super class variable can be used to refer the sub class object?

The super variable is not a reference to a subclass. It is a reference to the superclass.

class MyClass {

void printType() {

System.out.println("This is a MyClass");

}

// MySubClass is a subclass of MyClass. Within this class, the super keyword

// refers to MyClass.

static class MySubClass extends MyClass {

void printType() {

// Tell Java we also want to call the printType method of the super class

super.printType();

System.out.println("This is a MySubClass");

}

}

}

What is the difference between Java 1.6 and 1.7?

Java 1.7 introduces a lot of small improvements including some new language enhancements. Language enhancements include:Strings in switch statements, try-with-resources statements, improved type inference for generic instance creation ("diamond"), simplified varargs method invocation, better integral literals, and improved exception handling (multi-catch).

Example of diamond operator: List o = new ArrayList<>[42];


Java 1.7 also included support for dynamic languages.


Jave 6 was mostly incremental improvements to existing libraries, no new language features (except for the @Override).


For full list see Java Programming Language Enhancements.

How do you close a frame when opening another frame in java?

There are many ways to accomplish this but one way I might accomplish this is with a WindowListener or WindowAdapter. Below is a skeleton structure:

package pkg;


public class ExampleMainFrame extends JDialog

{

public ExampleMainFrame(final Window parent)

{

super(parent);

// ... Do other stuff for initialization here or in another internal method


addWindowListener(new WindowAdapter()

{

public void WindowClosed(WindowEvent e)

{

(new ExampleDialog2(parent)).setVisible(true);

}

}

}


// ... Other methods

}


(In different .java source file)

package pkg


public class ExampleDialog2 extends JDialog

{

public class ExampleDialog2(final Window parent)

{

super(parent);


// ... Do other stuff for initialization here or in another internal method

}


// ... Other methods

}



Explanation:

WindowListener is an Interface - WindowAdapter is a class that defines empty method definitions for each of the methods so you can effectively define/override one of the methods and instantiate a WindowAdapter and it won't do anything but what you've overridden. In this example, I've overridden WindowClosed and upon that event occuring, it'll create and show the 2nd frame.

What is the name of the class of people in their nineties?

Presumably "nonagenarians", by extension from "octagenarians" (people in their eighties), although I have never heard the term in actual use.

What is type safety in java?

Type safety is the degree to which the language prevents you from making typing errors. That is: assigning values of one type to variables of another type.

For example, in Java:

int i;

String s = "Not an integer";

i = s;

is a type error as you are trying to assign a value of type String to a variable of type int.

Java is moderately type safe as it will allow you to "type-cast" (automatically convert types) for some combinations but will give a compiler error for others.

What are the various character extraction function available in java?

JAVA provides a String class that provides the methods to extract the characters out of the given particular string from a String object. Now we shall discuss all the methods provided by JAVA in this blog.

charAt()To extract a particular character from the particular location from the string, charAt()method is used. General form:1char charAt(int where);

Here, where is the index of the character tobe extracted from the string. For choosing the value of where we should keep only one thing in mind that the value should be a nonnegative value. This method returns the character at that particular location.

Example:1public class exam{

2public static void main(String args[]){

3String s="abc";

4char ch;

5ch=s.charAt(2);

6System.out.println("charAt returns: "+ch);

7}

8}

Output of above code is:

getChars()getChars() is used to extract more than one characters at a time. General form:1void getChars(int sourceStart,int sourceEnd,char target[],inttargetStart);

Here,sourceStart specifies the start index of the substring, sourceEnd specifies an index that is one past the end of the desired substring. But the substring contains the characters 1 less than the actual value of sourceEnd. target[] will store the required characters. The index of the target array where the substring is to be stored is specified by the targetStart.

Example:01public class exam1{

02public static void main(String args[]){

03String s="Have a good day every one!!";

04int start=4;

05int end=11;

06char ch[]= new char[end-start];

07s.getChars(start,end,ch,0);

08System.out.println(ch);

09}

10}

Output of above code is:

getBytes()getBytes() stores the characters in an array of bytes, and it uses default character-to-byte conversions. General form:1byte[] getBytes();

It is most useful when you are a String value into an environment that does not support 16-bit Unicode characters.

toCharArray()toCharArray() is used to convert all characters in a String object into a character array. It returns an array of characters for the entire string. General Form:1char[] toCharArray();

The Do-While loop is what type of loop?

The do loop is similar to the while loop, except that the expression is not evaluated until after the do loop's code is executed. Therefore the code in a do loop is guaranteed to execute at least once. The following shows a do loop in action:

do {

System.out.println("Inside do while loop");

} while(false);

The System.out.println() statement will print once, even though the expression evaluates to false. Remember, the do loop will always run the code in the loop body at least once. Be sure to note the use of the semicolon at the end of the while expression.

What does java security do?

Defines the security policy for each application .Every Java Application can have its own Security manager.

What does import javax.swing.jaframe mean?

Its a Java programming language call to include a method called Jaframe which is is the Javax.Swing class Library.

What are the differences between gc methods of System and Runtime Classes in Java?

There is actually no difference between System.gc() and Runtime.gc(). They both essentially invoke the JVM's Garbage Collection operation.

The System.gc() is a static method so it's a little bit more convenient to use. The call System.gc() is effectively equivalent to the call:

Runtime.getRuntime().gc()