Jump to content

Java Programming/Q&A

From Wikibooks, open books for an open world


Navigate Project Page topic: v  d  e )
Welcome to the Java Programming questions and answers page.


Feel free to post any questions you have while learning Java Programming. If you have questions about this book, post them on the Java Programming discussion page. If you know the answer to a question, or you can improve any, go ahead and do it.


Since Java 1.5 a new feature was added to the Java language called Generic. It is similar to C++ Templates. Why the name Generic was given to this feature? Anybody who knows??
Generics come from other languages, see article on w:Generic programming for more information.
Generics come from other languages, see article on w:Generic programming for more information.
What is Corba? What are the components of Corba and also discuss the working of corba? What is the difference between RMI and CORBA?
CORBA is language independent "Remote Method Invocation" like RMI. RMI only works with Java, CORBA can be used with any languages. For more information see: w:Common Object Request Broker Architecture.

RMI is much simpler to use than CORBA, so in an only Java environment RMI should be used.

If you need to call methods between different language environments, use CORBA. With CORBA a Java client can call C++ server and/or a C++ client can call a Java server. With RMI that can not be done.
CORBA is language independent "Remote Method Invocation" like RMI. RMI only works with Java, CORBA can be used with any languages. For more information see: w:Common Object Request Broker Architecture.

RMI is much simpler to use than CORBA, so in an only Java environment RMI should be used.

If you need to call methods between different language environments, use CORBA. With CORBA a Java client can call C++ server and/or a C++ client can call a Java server. With RMI that can not be done.
Can Vector support nonhomogenious data?
The Vector class can receive any object (so it can not receive primitive types), even different types of data in the same Vector.

The Vector and Hashtable classes are legacy classes that come from the 1.0 version of Java and have enforced synchronized methods resulting in performance issues on systems that does not require this characteristic. Additional classes were added at 1.2, those are ArrayList and HashMap, without synchronization. Both syncronized and not syncronized versions are implementing the List and Map interfaces, respectivelly.

Since Java 1.5, there is Generics, which allows you to specify that you want a container, like an ArrayList, to contain only certain types of things. This allows you to guarantee that what you are getting out of the container is a certain type at compile time, without explicitly casting it. This is safer (you avoid cast errors at run time) and makes your code more readable and understandable. You should use generics whenever possible.
The Vector class can receive any object (so it can not receive primitive types), even different types of data in the same Vector.

The Vector and Hashtable classes are legacy classes that come from the 1.0 version of Java and have enforced synchronized methods resulting in performance issues on systems that does not require this characteristic. Additional classes were added at 1.2, those are ArrayList and HashMap, without synchronization. Both syncronized and not syncronized versions are implementing the List and Map interfaces, respectivelly.

Since Java 1.5, there is Generics, which allows you to specify that you want a container, like an ArrayList, to contain only certain types of things. This allows you to guarantee that what you are getting out of the container is a certain type at compile time, without explicitly casting it. This is safer (you avoid cast errors at run time) and makes your code more readable and understandable. You should use generics whenever possible.
How can I run Java programs on a PC?
Java applications can be run in the same way as any other program if they are found in .JAR or .JNLP packages. If you are trying to run a .class file, simply open a command line prompt and type java MyClass, where MyClass is the name of the class file you are trying to launch.
Java applications can be run in the same way as any other program if they are found in .JAR or .JNLP packages. If you are trying to run a .class file, simply open a command line prompt and type java MyClass, where MyClass is the name of the class file you are trying to launch.
What are the equivalent String functions in Java to the functions LEFT(), MID(), RIGHT() in BASIC?
The Java String class provides substring() methods that should provide what you need. For example, mid(str, a, b) in Visual Basic would be something like str.substring(a, a+b).
The Java String class provides substring() methods that should provide what you need. For example, mid(str, a, b) in Visual Basic would be something like str.substring(a, a+b).
What is a class and how does it accomplish data hiding?
A class is a template to create objects. Methods and variables declared private can not be access outside of the class. Those can be only accessed internally, it means that they are hidden from the outside. It makes programming esier because it is garantide that those internal information won't be messed up by outside code.
A class is a template to create objects. Methods and variables declared private can not be access outside of the class. Those can be only accessed internally, it means that they are hidden from the outside. It makes programming esier because it is garantide that those internal information won't be messed up by outside code.
Can a keyword used for variable name?
No. Keywords are resirved by the compiler and can not be used for variables.
No. Keywords are resirved by the compiler and can not be used for variables.
What are the jump statements available in Java?
Java does not have jump statements.

The goto is reserved, but not used.

The break statement is close to a jump statetement, but the destination of the jump is well defined. It breaks/jumps out from a loop, or a switch statement.
Java does not have jump statements.

The goto is reserved, but not used.

The break statement is close to a jump statetement, but the destination of the jump is well defined. It breaks/jumps out from a loop, or a switch statement.
What is automatic type conversion?
Can a constructor be private?
Yes. Singleton constructors are private. When the constructor is private no object of that class can be created outside of that class. How and when an object can be created of that class is defined by that class.
Yes. Singleton constructors are private. When the constructor is private no object of that class can be created outside of that class. How and when an object can be created of that class is defined by that class.
What is the differance between composition and aggregation?
Why a char uses 2 bytes of memory in java while it occupies only one byte in C++?
Java uses unicode, while C++ uses ASCII. Unicode consists of a repertoire of about 100,000 characters, which is much more than what the ASCII character-set can represent. See also Java Programming/Syntax/Unicode Source
Java uses unicode, while C++ uses ASCII. Unicode consists of a repertoire of about 100,000 characters, which is much more than what the ASCII character-set can represent. See also Java Programming/Syntax/Unicode Source
how we get the values in java programming?
You can display the value of any variable by using the System.out.print() and System.out.println() functions.
You can display the value of any variable by using the System.out.print() and System.out.println() functions.
What is the introduction of java language?
The java language is introduced from here. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
The java language is introduced from here. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
How i can open a java file from cd? How objects are created from a class? out is an object of class PrintStream then why Java define out in System class? For printing message we use System.out.print() method, if out is defined in PrintStream we can also use that method like PrintStream.out.print()?
  • A Java file located on a CD should be opened the same way as a Java file on a hard disk. You should use the path of the file on the CD. If you want to open a file from the command line or a prompt, use the cd command to move to the folder and open your Java file as you would open a text file.
  • Do not confuse an object of a class and a field of a class. out is a field of the System class and out is an object of the PrintStream class.
  • Only out in the System class is linked to the standard output at Java startup. That's why you must use the System class.
Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
  • A Java file located on a CD should be opened the same way as a Java file on a hard disk. You should use the path of the file on the CD. If you want to open a file from the command line or a prompt, use the cd command to move to the folder and open your Java file as you would open a text file.
  • Do not confuse an object of a class and a field of a class. out is a field of the System class and out is an object of the PrintStream class.
  • Only out in the System class is linked to the standard output at Java startup. That's why you must use the System class.
Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
Hi Friends. Could you please tell me in detail about the JVM Generations. In the line "System.out.println()" what is exact meaning of System & out? How declare class in java? Thanks & regards Hema26
The JVM is not generated. You download the JRE or JDK and once installed you launch the JVM as explained here. System is a class. It's a static use of a class. out is a static field of the System class. The book explain how to create a class here. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
The JVM is not generated. You download the JRE or JDK and once installed you launch the JVM as explained here. System is a class. It's a static use of a class. out is a static field of the System class. The book explain how to create a class here. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
How to trace a simple java program?
The simplest way to log your code is to print in the standard output, that is to say to use System.out.println();. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
The simplest way to log your code is to print in the standard output, that is to say to use System.out.println();. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
Write java program which calculate the shape of triangle, square, and rectangle on three classes? Write a java program to enter 2 numbers and perform all the basic operation of calculator?
To calculate the perimeter or the area of a shape, use the methods of the java.lang.Math class as explained here. To draw sush shapes, you can use Swing as described here. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
To calculate the perimeter or the area of a shape, use the methods of the java.lang.Math class as explained here. To draw sush shapes, you can use Swing as described here. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
I would like to get the details of method main() and a() by using T class reference.
Using Eclipse, you can access to the method declaration clicking on it pressing the Ctrl key. You can access to the features of a class in your program using Reflection. BlueJ is not the most popular IDE, so there is not much information about it in the IDE section. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
public class A{
 public void a(){
  System.out.println("Hello1");
 }
}

public class T{
 public static void main(String... args){
  A a = new A();
  a.a();
 } 
}
Using Eclipse, you can access to the method declaration clicking on it pressing the Ctrl key. You can access to the features of a class in your program using Reflection. BlueJ is not the most popular IDE, so there is not much information about it in the IDE section. Ftiercel (discusscontribs) 19:29, 11 March 2013 (UTC)
public class A{
 public void a(){
  System.out.println("Hello1");
 }
}

public class T{
 public static void main(String... args){
  A a = new A();
  a.a();
 } 
}
How to create and run a .jar program in BlueJ?
Just go to the menu "export project" and select this format.
Just go to the menu "export project" and select this format.

Programming code..

[edit | edit source]

What is the code that one can use in java to display height in feet (int) and inches(int)?

(!)Why does this not work plz help ASAP(!)

[edit | edit source]

import java.util.Random; import java.util.Scanner; class Main {

 public static void main (String[]args)
 {
   Scanner scanner = new Scanner (System.in);
   Random random = new Random ();
   {
     int rock = 0;
     int paper = 0;
     int scissors = 0;
     int loop = 0;
     int no = 0;
     while (loop == 0)

no = random.nextInt (3) + 1;

     if (no == 1)

rock = 1;

     if (no == 2)

paper = 1;

     if (no == 3)

scissors = 1;

   }
   System.out.
     println ("Pick rock paper scissors\nRock = 1 Paper = 2 Scissors = 3: ");
   int cho = scanner.nextInt ();
   {
     if (cho == 1 & no == 3 | cho == 2 & no == 1 | cho == 3 & no == 2)

{ System.out.println ("You Win"); loop++; }

     if (cho == 1 & no == 2 | cho == 2 & no == 3 | cho == 3 & no = 1)

{ System.out. print ("You loose want to play again? Type 0 to play again or type 1 to Exit: "); loop = scanner.nextInt (); }

     if (cho == 1 & no == 1 | cho == 2 & no == 2 | cho == 3 & no == 3)

System.out.println ("Tie try again");

   }
 }

}

Write a program that calculates the current checking account balance, based on user input.


Inputs:

Ask user for account number and name.

Ask user for opening balance.

Ask user for up to 5 debit transactions, user can stop entering by typing -1.

Ask user for up to 5 credit transactions, user can stop entering by typing -1.


Processing:

Calculate new balance.


Output:

[Account number] - [Customer first name] [Customer last name]

Original Balance: [Original balance]

Debits: [Number of debits]

Debit Total: [Debit total]

Credits: [Number of credits]

Credit Total: [Credit total]

Balance: [Current account balance]