Home >> Feeds Directory >> Computers >> Programming >> Java Programming

Java Programming

Top picks for today

Java programming FAQ
FAQs about Java programming, how to call one object from another, exceptions, OS, domain name and static context queries.
  • How do I call another class in the same folder?
    Assuming the Java classes are in the same package, one class should instantiate the other to call an instance method, or use a class name reference to call a static method, as below.

  • How do I add two numbers in Java?
    A: To add two numbers in Java, use the simple mathematical plus operator, which may be applied to any numeric variable type, as below:

  • How can I add two numbers using a method?
    A: For simple mathematical functions a method would usually declare the input variables as method arguments and a return type that matches the required numeric type for the result. In the simplest case an add(int, int) method would return an int value, as below:

  • How should I round a double up to an int?
    A: To round a double value up and convert it to an int value takes two operations. The static Math.ceil(double) method returns a double value that is equal to the "next highest" integer. You must then give an explicit down cast to an int, as below.

  • How do I get the name of the operating system?
    A: To get the operating system name in Java use the static System.getProperty("os.name") method, which returns a string. Other operating system property keys are "os.arch" for the hardware architecture and "os.version" for the version number.

  • What is a recursive method?
    A: A recursive method is one whose method body includes a call to itself, so that it is called repeatedly until an expected condition is met or it cannot continue the recursion any longer. These methods often take an object or numeric argument that is subject to progressive interrogation or mathematical processing at each pass. Recursive methods must be designed carefully to ensure that they do not result in a very deep or endless recursion, which is likely to cause an OutOfMemoryError.

  • What is a good beginner's book about Java?
    A: Thinking In Java by Bruce Eckel is a good beginner's book on Java, which is available as a free download too.

  • Can you give an example of catching an exception?
    A: One common example of catching an exception is when you try to read from a file that may not exist. You may enter a file name as the first argument on the command line, for example:

  • How can I throw a NumberFormatException back to the main() method?
    A: NumberFormatException is an un-checked exception. That means that the compiler will not enforce that your application catches the exception and handle the case. For example, users may enter an invalid number at runtime and the application would throw an exception and crash.

  • How do Java front- and back-end skill sets differ?
    A: This is a somewhat arbitrary distinction to make about Java development, since applications often have front- and back-end components and it is important to understand how both aspects are integrated. In general terms, back-end development is concerned with database storage and retrieval, servlets, Web application frameworks and Enterprise Java Beans. This requires a good understanding of SQL and database applications, JDBC, network principles, servlet containers, the HTTP protocol and an appreciation of concurrent programming issues.

  • How can I make spelling checker with a LinkedList dictionary?
    A: A basic spelling checker that processes an input stream of some kind might use a StreamTokenizer to identify the words from the input and use the LinkedList's contains(Object) method to check whether the given word is recognised. How you deal with un-matched words is up to the application interface, this example just lists the line number and word on the console output.

  • How can I use SAX to parse a Web page via JTidy?
    A: The JTidy project help page is not all that helpful. The original HTML Tidy configuration options are a better reference to the equivalent methods in the Java implementation and the Code Style JTidy development notes article should help you get started.

  • How can I create a search engine in Java?
    A: This is a big question and there are many aspects to consider. You may find it helpful to take a look at the MKSearch system. This is an open source project, so you can review the code yourself. The project Web site includes Java documentation, configuration notes and how to guides to get you started.

  • How can I design my own fonts using Java?
    A: It is puzzling that you would want to design fonts using Java, the language is not particularly suited for this purpose. There are several very good applications for designing and converting fonts such as Fontographer for example.

  • How can I download a Web page and write it to a file?
    A: The standard Java input/output and network APIs make it quite easy to acquire content from the Internet and write files. Writing from a stream input to a file output is the slightly complex part. The example below uses a copy(InputStream, OutputStream) method adapted from a version in Java I/O by Elliotte Rusty Harold.

  • How do I get the domain name from a URL?
    A: If you want to extract a domain name from an existing URL object, use the URL's getHost() method.

  • How do I get a domain name from an IP address?
    A: It is possible to do a reverse name look-up for an IP address using the java.net.InetAddress class. The example below first gets an instance of the class using the static getByName(String) method, then applies the getHostName() method to it. The method will only resolve a domain name if there is one configured for the host, otherwise the method returns the original IP address.

  • My Java Web client gets the wrong site!
    A: Many Web sites use virtual hosting, which requires you send an HTTP Host header with your request.

  • How can I set a code name identifier for my feed reader?
    Your application should pass a User-Agent header in the HTTP request with the code name of your application as the header value. Web browser user agents often include the full version and build number for the application and the software platform it was built for. Feed readers tend to use simpler name and version number combination. See the Java example below.

  • What is special about static variables?
    A: A static variable is shared by all instances of a class. Every instance has a reference to the same variable and can modify it directly. Static instances are declared with the static modifier.


Newsfeed display by CaRP
Home  ::  RSS Feeds Directory  ::  RSS Feed Reader  ::  Add RSS Feed  ::  Sitemap

Send email or call Arun (+91-98310-27107) for Joint Ventures