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

How do you write and invoke methods that take parameters?

// This method will accept an integer, double, and string as parameters

// To invoke it, simply call testMethod(0, 1.1, "hello");

// (or whatever values you wish to substitute in)

void testMethod(int param0, double param1, String param2) {

System.out.println("Accepting parameter: " + param0);

System.out.println("Accepting parameter: " + param1);

System.out.println("Accepting parameter: " + param2);

}

What is the difference between catch exception e catch error err and catch throwable t?

In Java it is related to the class hierarchy of exceptions. Throwable is the root object of the heirarchy, and both Exception and RuntimeError subclass it. Methods include a "throws" clause in their signature to indicate errors of type "Exception" that can be thrown in the body of the method and returned to the caller. Errors of type RuntimeError do not need to be reported in the throws clause.


Exceptions are also called "checked" errors, and RuntimeErrors are called "unchecked." This simply means that methods that throw checked errors must declare them in the signature.


* Exception: this is a "checked" error. Usually, this is some sort of data error that can be handled by the method. An example is FileNotFoundException - some code didn't an expected file, but the program may be perfectly capable of going on.

* Error: A more serious problem that a program probably can't deal with. An example is OutOfMemoryError - when there is no more memory, there isn't much you can do about it and not much point in continuing.

* Throwable: Superclass of both Error and Exception.


So the main difference is that

* 'catch Exception' will catch just exceptions (the problems you might deal with)

* 'catch RuntimeError' will catch just errors (the problems you probably can't deal with)

* 'catch Throwable' will catch all errors.


Generally, you want to catch Exception or Throwable, but not RuntimeError. You will only want to catch Throwable if there is a requirement that the method never throw an error back to the caller.


See related link to the Java API, and browse the documentation and subclasses of Throwable.

What is syntax errors in java?

Runtime errors in Java means that there is some code error existing in your computer and it is impossible for you to deal with it by yourself unless you are the you're the applet's designer and change the code. Most of the time, runtime errors in Java will appear error message just like:

"Java(TM) Plug-in Fatal Error

Several Java Virtual Machines running in the same process caused an error."

"Java(TM)Plug-in Fatal ERror

the Java Runtime Environment cannot be loaded."

How Write a program to calculate sum of numbers between 0 to 10 in ruby on the rail?

I don't specifically know Ruby on Rails, but usually (in most programming languages), to do this task you would define one variable for the sum, and one for a counter. You initialize the sum to zero, and then, in a loop, you (1) increment the counter (most languages specifically have a "for" loop for this), and (2) add the counter to the sum. Here is the pseudocode; it is NOT for a specific programming language, but it gives you the general idea; you should be able to adapt it to most programming languages:

sum = 0

for addend = 1 to 10

{

sum = sum + addend

}

output "Sum = ", sum

Note that you may also need to initialize the variables, depending on the language.

What method of purchasing is a cost containment method?

A cost containment method for purchasing is bulk buying, where organizations purchase large quantities of goods or services at a discounted rate. This approach reduces per-unit costs, lowers inventory expenses, and can lead to better supplier negotiations. Additionally, implementing a just-in-time (JIT) purchasing strategy can minimize holding costs and reduce waste by ensuring that inventory arrives only as needed. Both methods help organizations manage expenses effectively while maintaining necessary supplies.

How are you in ciphertext?

There are diverse ways to encrypt information (convert it to cyphertext); how a text will look in cyphertext will depend on the method chosen, as well as the specific keys.

Java does not support pointers then what is null pointer exception?

A null pointer exception is thrown when you are trying to manipulate an object that is null. It is just the name and does not have any relevance to the pointers as in C

Example:

ArrayList lst = null;

Object obj = lst.get(0);

In the first line we have declared an array list. Without initializing it we have tried to access the element in the 0th position. This would cause a null pointer exception.

What will happen if you will assign a value to float variable which is beyond the range of float data type?

Then data will be lost. Quite often, at least in Java, the compiler will protest at compile time, basically forcing you to rethink your strategy.

What are the native methods how do you use them?

The native modifier indicates that a method is implemented in platform-dependent code (Not Java Of Course), but often in C. There wont be any questions about how to use native methods for the exam. For now it is enough if you know that native is a modifier (thus a reserved keyword) and that native can be applied only to methods-not classes, not variables, just methods. Note that a native method's body must be a semicolon (;) (like abstract methods), indicating that the implementation is omitted.

What are Integer Boolean and Double wrapper classes?

In Java an integer and a double are "primitive" data types.

When dealing with functions, often you want to do more than send an "int" or a "double"'s VALE, you wish to send a REFERENCE of a number type to a function so that you can operate on that number type. What you can do is create a new Integer, Boolean, or Double, and send it to the function. This creates a "CLASS" that acts as an Integer (or whatever you chose).

Eg. You want a function that takes in a number variable, and adds two to it, and returns NOTHING. You cannot send a normal "int", because that integer cannot be "referenced" to after the function call. You would create a new Integer Object from the Integer Class, and insert that new object to the function.

Integer four = new Integer(4); // Makes a new Integer named "four" with the value 4.

There is a wrapper class for every primitive in Java. For instance, the wrapper class for int is Integer, the class for float is Float, and so on. Remember that the primitive name is simply the lowercase name of the wrapper except for char, which maps to Character, and int, which maps to Integer.

The Wrapper classes for each of the primitive types is as follows:

1. boolean - Boolean

2. byte - Byte

3. char - Character

4. double - Double

5. float - Float

6. int - Integer

7. long - Long

8. short - Short

A point to note here is that, the wrapper classes for numeric primitives have the capability to convert any valid number in string format into its corresponding primitive object. For ex: "10" can be converted into an Integer by using the below line

Integer intVal = new Integer("10");

I can do the same for all numeric types like long, short etc. But, if I pass an invalid value I will get a "NumberFormatException" Ex:

Integer intVal = new Integer("Haha");

The Wrapper Constructors

All of the wrapper classes except Character provide two constructors: one that takes a primitive of the type being constructed, and one that takes a String representation of the type being constructed-for example

Integer i1 = new Integer(42);

Integer i2 = new Integer("42");

or

Float f1 = new Float(3.14f);

Float f2 = new Float("3.14f");

The Character class provides only one constructor, which takes a char as an argument-for example

Character c1 = new Character('c');

The constructors for the Boolean wrapper take either a boolean value true or false, or a String. If the String's case-insensitive value is "true" the Boolean will be true-any other value will equate to any other value will equate to false. Until Java 5, a Boolean object couldn't be used as an expression in a boolean test-for instance

Boolean b = new Boolean("false");

if (b) // won't compile, using Java 1.4 or earlier

As of Java 5, a Boolean object can be used in a boolean test, because the compiler will automatically "unbox" the Boolean to a boolean.

What is user thread?

A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently of everything else that might be happening.

What do you mean by applet?

An applet is a piece of code that can be used for graphical user interaction. An applet generally uses java api's for applets. It could be a window/ a dialog box and some such gui.

Visit www.sun.com for examples on applets

How objects are predefind types?

An object is created from a class, like a house made from a blueprint. The object will therefore be of the type of its class. For instance, a String object will be of type String, which is defined by the String class.

What is a specifier?

a specifier tells the JVM how to treat a particular class,method and variable while executing the program. For example, final classes cannot be extended and final methods cannot be overriden and final variables cannot be changed once declared. Likewise,static methods and variables can be accessed without having to instantiate an object for their class

What is the sacratic method?

The Socratic method is a form of cooperative dialogue that encourages critical thinking through asking and answering questions. Named after the ancient Greek philosopher Socrates, it involves stimulating discussion by challenging assumptions and exploring ideas in depth. This method often leads participants to discover answers for themselves, promoting deeper understanding and insight. It's frequently used in educational settings to foster analytical skills and independent thought.

What is the function of keyword extern?

The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. Declarations of variables and functions at file scope are external by default. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s). C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit. Microsoft C++ supports the strings "C" and "C++" in the string-literal field. All of the standard include files use the extern "C" syntax to allow the run-time library functions to be used in C++ programs. If you find this info useful Please vote!!!

How do you write exponents in Java?

You can use the Math.pow function to raise one number to the power of another.

Math.pow(24, 3) will return 243

What are some sites that you can code and save java online other than Compilr.com?

You should seriously consider getting an IDE, and writing code off-line.

<><><><><>

There are several free IDE's, including Microsoft's Visual Studio and Sun's Eclipse.

What is the technical name for calling a base class constructor using derived class constructor?

You cannot actually call any constructor, you can only invoke construction, either by instantiating a static instance of a class or by dynamically creating one with the new operator. In the case of base class construction via a derived class, the base class constructor is invoked by the derived class' initialisation list.

Every class constructor has an initialisation list whether you define one or not (the compiler will generate one automatically if you don't). When you derive one class from another, the derived class initialisation list invokes a call to the base class default constructor. The only exception is the compiler-generated copy constructor which automatically calls the base class copy constructor.

If you define your own initialisation list, then you can explicitly invoke any base class constructor overload, thus making your construction code all the more efficient. However, copy constructors should always invoke the base class copy constructor, so if you define a copy constructor, you must explicitly invoke the base class copy constructor -- the compiler will not invoke it implicitly from a user-defined copy constructor.

While many programmer's use the constructor's body to initialise a class, this is highly inefficient. Even if you don't specify an initialisation list, one is created for you, resulting in every base class and every member variable being initialised twice, which can quickly add up to a substantial cost in performance.

The constructor's body should only really be used for initialisation when it would be difficult or impossible to do so from the initialisation list. Remember that your object doesn't physically exist until initialisation is complete, so you may not have access to some members, particularly base class members, at certain points in the initialisation process.

Initialisation must be done from the ground up, starting with the base classes and ending with the actual class members, and all in the order they were declared. Note that only direct base classes (or virtual base classes) should be invoked from the initialisation list. The base classes themselves should invoke their own base class constructors, if they have any. Thus no matter which derivative you construct, the least-derived class is always constructed first.

Why should indexes be used instead of subscripts to identify array elements?

You cannot uses indices instead of subscripts. The subscript operator [] requires an index in order to determine the subscript. Even if you don't use the subscript operator you still need an index to determine the offset of the subscript. Indeed, the only time you do not need an index is when traversing the array using a roving pointer, which is arguably more efficient than using a subscript to traverse an array since subscripts use multiplication instead of the much simpler increment/decrement operation.