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

What is function overloading in java?

Any function or method in Java that is coded by the programmer is called a user defined method in Java. The JAVA API (Application Programming Interface) has a set of predefined classes & methods that are for our usage. Whatever methods we create apart from these are termed as user defined methods. In java we not use the term functions. We call them "Methods"

What is the difference between String and String Buffer?

In Java, String is a class that represents an immutable string, that is a sequence of characters that can never change. Any modification to the String will have to create a new String object. A StringBuffer (and in Java 1.5 and up, a StringBuilder) is a mutable string object that can be modified at runtime.

The advantages of using Strings are that the String is generally lighter and faster to use where the String isn't going to change. When building a long String of text by making changes to one object over time, you should use a StringBuilder or StringBuffer (in Java 1.4 or when synchronization is important).

3 pillars of object oriented programming?

abstraction, inheritance, encapsulation, and polymorphism.

What is class variable?

Class Variables or Instances variables are variables that are declared inside a class and are available for the whole class. They are available for all instances of that class and are not specific to any method. Ex: public class Test { private String name = "Rocky"; } Here name is a class variable.

What is collection in object oriented programing?

The Java Collection interface is the superinterface for any class in Java which represents a group of objects.

Some types of collections:

  • Dequeues
  • Lists
  • Maps
  • Queues
  • Sets
  • Stacks
  • Vectors

Why using the name main for main method in java?

If you run an java file(as an .class or .jar file) there's always 1 method being called: The main(String[] args) method.

The method is only called once.

Example of an main method:

public static void main(String args[]) throws IOException {

LoggingBootstrap.bootstrap();

gui = new GUI();

}

In this case it only bootstraps the logger and uses the constuctor method of GUI(the graphical user interface of the program)

What are the steps to increase java heap space on a server?

You can specify the starting and maximum heap sizes when you launch a Java program by using the command line switches:

-Xms<size> set initial Java heap size

-Xmx<size> set maximum Java heap size

Example: The following line will run the MyProgram Java program with 64-128mb heap space.

java -Xms64m -Xmx128m MyProgram

How do you declare an object reference variable?

For an object named Point you would declare an object reference variable like this:

Point p1;

The name p1 can be any valid variable name that you want. Note that p1 does not yet refer to an actual object yet, you have just created a reference to one (kind of like a pointer in C). To set p1 to actually refer to an instance of the object Point you could do something like this:

p1 = new Point();

Types of array in data structures?

There are many types of trees:

- binary trees

- binary search trees

- B+ trees

- red-black trees

- AVL trees

- suffix trees

- and much more....

Each have different rules for adding a new element, removing an element, and searching for an element in the tree. Consequently, they all have different advantages and disadvantages.

How polymorphism is used in java?

Polymorphism is the feature that allows one interface to be used for general class actions.

class CPolygon {

protected:

int width, height;

public:

void set_values (int a, int b)

{ width=a; height=b; }

virtual int area (void) =0;

void printarea (void)

{ cout << this->area() << endl; }

};

class CRectangle: public CPolygon {

public:

int area (void)

{ return (width * height); }

};

class CTriangle: public CPolygon {

public:

int area (void)

{ return (width * height / 2); }

};

int main () {

CPolygon * ppoly1 = new CRectangle;

CPolygon * ppoly2 = new CTriangle;

ppoly1->set_values (4,5);

ppoly2->set_values (4,5);

ppoly1->printarea();

ppoly2->printarea();

delete ppoly1;

delete ppoly2;

return 0;

}

By

MUTHU

What does 'API' stand for in Java?

According to Wikipedia:

"An API is an abstraction that defines and describes an interface for the interaction with a set of functions used by components of a software system. The software that provides the functions described by an API is said to be an implementation of the API."

Read more, below.

Why were high-level languages developed?

Because machine-language programming is extremely time-consuming, programmers began using English-like abbreviations to code the programs. High-level languages were developed to improve programming efficiency by requiring fewer coding statements.

What are the feature of java?

Java was developed by taking the best points from other programming languages, primarily C and C++. Java therefore utilises algorithms and methodologies that are already proven. Error prone tasks such as pointers and memory management have either been eliminated or are handled by the Java environment automatically rather than by the programmer. Since Java is primarily a derivative of C++ which most programmers are conversant with, it implies that Java has a familiar feel rendering it easy to use.

JAVA IS OBJECT-ORIENTED

Even though Java has the look and feel of C++, it is a wholly independent language which has been designed to be object-oriented from the ground up. In object-oriented programming (OOP), data is treated as objects to which methods are applied. Java's basic execution unit is the class. Advantages of OOP include: reusability of code, extensibility and dynamic applications.

JAVA IS DISTRIBUTED

Commonly used Internet protocols such as HTTP and FTP as well as calls for network access are built into Java. Internet programmers can call on the functions through the supplied libraries and be able to access files on the Internet as easily as writing to a local file system.

JAVA IS INTERPRETED

When Java code is compiled, the compiler outputs the Java Bytecode which is an executable for the Java Virtual Machine. The Java Virtual Machine does not exist physically but is the specification for a hypothetical processor that can run Java code. The bytecode is then run through a Java interpreter on any given platform that has the interpreter ported to it. The interpreter converts the code to the target hardware and executes it.

JAVA IS ROBUST

Java compels the programmer to be thorough. It carries out type checking at both compile and runtime making sure that every data structure has been clearly defined and typed. Java manages memory automatically by using an automatic garbage collector. The garbage collector runs as a low priority thread in the background keeping track of all objects and references to those objects in a Java program. When an object has no more references, the garbage collector tags it for removal and removes the object either when there is an immediate need for more memory or when the demand on processor cycles by the program is low.

JAVA IS SECURE

The Java language has built-in capabilities to ensure that violations of security do not occur. Consider a Java program running on a workstation on a local area network which in turn is connected to the Internet. Being a dynamic and distributed computing environment, the Java program can, at runtime, dynamically bring in the classes it needs to run either from the workstation's hard drive, other computers on the local area network or a computer thousands of miles away somewhere on the Internet. This ability of classes or applets to come from unknown locations and execute automatically on a local computer sounds like every system administrator's nightmare considering that there could be lurking out there on one of the millions of computers on the Internet, some viruses, Trojan horses or worms which can invade the local computer system and wreak havoc on it.

Java goes to great lengths to address these security issues by putting in place a very rigorous multilevel system of security:

  • First and foremost, at compile time, pointers and memory allocation are removed thereby eliminating the tools that a system breaker could use to gain access to system resources. Memory allocation is deferred until runtime.
  • Even though the Java compiler produces only correct Java code, there is still the possibility of the code being tampered with between compilation and runtime. Java guards against this by using the bytecode verifier to check the bytecode for language compliance when the code first enters the interpreter, before it ever even gets the chance to run.

    The bytecode verifier ensures that the code does not do any of the following:

    • Forge pointers
    • Violate access restrictions
    • Incorrectly access classes
    • Overflow or underflow operand stack
    • Use incorrect parameters of bytecode instructions
    • Use illegal data conversions
  • At runtime, the Java interpreter further ensures that classes loaded do not access the file system except in the manner permitted by the client or the user.

Sun Microsystems will soon be adding yet another dimension to the security of Java. They are currently working on a public-key encryption system to allow Java applications to be stored and transmitted over the Internet in a secure encrypted form.

JAVA IS ARCHITECTURALLY NEUTRAL

The Java compiler compiles source code to a stage which is intermediate between source and native machine code. This intermediate stage is known as the bytecode, which is neutral. The bytecode conforms to the specification of a hypothetical machine called the Java Virtual Machine and can be efficiently converted into native code for a particular processor.

JAVA IS PORTABLE

By porting an interpreter for the Java Virtual Machine to any computer hardware/operating system, one is assured that all code compiled for it will run on that system. This forms the basis for Java's portability.

Another feature which Java employs in order to guarantee portability is by creating a single standard for data sizes irrespective of processor or operating system platforms.

JAVA IS HIGH-PERFORMANCE

The Java language supports many high-performance features such as multithreading, just-in-time compiling, and native code usage.

  • Java has employed multithreading to help overcome the performance problems suffered by interpreted code as compared to native code. Since an executing program hardly ever uses CPU cycles 100 % of the time, Java uses the idle time to perform the necessary garbage cleanup and general system maintenance that renders traditional interpreters slow in executing applications. [NB: Multithreading is the ability of an application to execute more than one task (thread) at the same time e.g. a word processor can be carrying out spell check in one document and printing a second document at the same time.]
  • Since the bytecode produced by the Java compiler from the corresponding source code is very close to machine code, it can be interpreted very efficiently on any platform. In cases where even greater performance is necessary than the interpreter can provide, just-in-time compilation can be employed whereby the code is compiled at run-time to native code before execution.
  • An alternative to just-in-time compilation is to link in native C code. This yields even greater performance but is more burdensome on the programmer and reduces the portability of the code.
JAVA IS DYNAMIC

By connecting to the Internet, a user immediately has access to thousands of programs and other computers. During the execution of a program, Java can dynamically load classes that it requires either from the local hard drive, from another computer on the local area network or from a computer somewhere on the Internet.

How do you make restaurant bill program in java?

import java.io.*;

class Bill

{

String item[]={"A","B","C","D","E","F","G","H","I","J"};

double rate[]={1,2,3,4,5,6,7,8,9,10};

String order[]=new String[10];

double amt[]=new double[10];

String ans;

int quantity[]=new int[10];

int i,n=0,no,x=0;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

Bill()

{

}

public void putOrder()throws IOException

{

System.out.println("**********************************************");

System.out.println("Kentucky Fried Chicken- MENU CARD");

System.out.println("*********************************************");

System.out.println("Item No. Item Rate");

for(i=0;i< 10;i++)

{

System.out.println((i+1)+ ": "+item[i]+"------------------"+rate[i]);

}

System.out.println("Enter item number to place your order\n");

while(true)

{

if(x==10)

break;

System.out.println("Enter item number:");

no=Integer.parseInt(br.readLine());

order[x]=item[no-1];

amt[x]=rate[no-1];

System.out.println("Enter quantity:");

no=Integer.parseInt(br.readLine());

quantity[x]=no;

x++;

if(x>0)

{

System.out.println("Any More Items ?(Y/N):");

ans=br.readLine().toUpperCase();

if(ans.equals("N"))

break;

}

}

showBill();

}

private void showBill() throws IOException

{

double bamt=0,b;

System.out.println("You have ordered\n");

System.out.println("Item Rate No. of quantity");

for(i=0;i< x;i++)

{

System.out.println((i+1)+":"+order[i]+"---------"+amt[i]+"------------"+quantity[i]);

}

while(true)

{

if(x< 2)

break;

System.out.println("Do you want to cancel any item (Y/N):");

ans=br.readLine().toUpperCase();

if(ans.equals("N"))

break;

System.out.println("Enter the item number of the item you want to cancel:");

no=Integer.parseInt(br.readLine());

for(i=no-1;i< x-1;i++)

{

order[i]=order[i+1];

amt[i]=amt[i+1];

quantity[i]=quantity[i+1];

}

x--;

}

System.out.println("Cash Memo\n");

System.out.println("*****************************************************************");

System.out.println("Item No. of quantity Rate Amount\n");

System.out.println("***************************************************************");

for (i=0;i< x;i++)

{

System.out.print( order[i]+ " "+quantity[i]+ " "+ amt[i]+ " ");

b=quantity[i]*amt[i];

System.out.println(b);

bamt=bamt+b;

}

System.out.println("Bill Amount Rs. "+bamt);

System.out.println("*******************");

bamt=bamt+bamt*.125;

System.out.println("Total Amount including service charge @ 12.5% Rs. "+bamt);

}

}

Write a program to generate the Fibonacci Series using array?

#include<stdlib.h>

#include<conio.h>

#include<stdio.h>

void main (void)

{

clrscr();

int i;

int a[10];

a[0]=0;

a[1]=1;

printf("First 10 Fibonacci numbers are :\n");

printf("%d\n%d\n",a[0],a[1]);

for(i=2;i<10;i++)

{

a[i]=a[i-1]+a[i-2];

printf("%d\n",a[i]);

}

getch();

}

What do you mean by a bean in java?

java beans provide business logic methods by which we manually call methods such as setter methods and getter methods in a encapsulated way of object oriented programming. Or even we can say these are model components in MVC architecture.

A system level project in java?

1.A web caching server in java. 2.VOIP

Distinguish between the concept assembler compiler and interpreter?

Compiler -- reads human-readable source code, produces machine-executable binary code. Examples are C, COBOL, Java, etc. Easiest for humans to program, but does not always produce the most efficient executables.

Interpreter -- Reads human-readable code, line at a time, and produces and executes machine instructions "on the fly". Example is good old BASIC. Good for testing, but is VERY slow.

Assembler -- Converts machine-manipulation coding directly into binary machine instructions. Produces the most efficient executables, but is the most difficult (for humans) to work with.

Best of all worlds (my opinion) is C on Unix, for speed, ease of use, program efficiency.

What does java do for a computer?

Java is Object Oriented computer programming language.

It is used for application as well as system software development.

It is best for the web based application such as servlets, XML design...etc.

I.e. the application can run in the Internet.

It can be used as front end tool for the back end database application. for example, the famous Oracle Database management system was designed using Java technology

It is platform independent. i.e. can run under most platforms and OSs.

Java technology was created as a computer programming tool in a small, secret effort called "the Green Project" at Sun Microsystems in 1991.

The secret "Green Team," fully staffed at 13 people and led by James Gosling, locked themselves away in an anonymous office on Sand Hill Road in Menlo Park, cut off all regular communications with Sun, and worked around the clock for 18 months.

They were trying to anticipate and plan for the "next wave" in computing. Their initial conclusion was that at least one significant trend would be the convergence of digitally controlled consumer devices and computers.

A device-independent programming language code-named "Oak" was the result.

To demonstrate how this new language could power the future of digital devices, the Green Team developed an interactive, handheld home-entertainment device controller targeted at the digital cable television industry. But the idea was too far ahead of its time, and the digital cable television industry wasn't ready for the leap forward that Java technology offered them.

As it turns out, the Internet was ready for Java technology, and just in time for its initial public introduction in 1995, the team was able to announce that the Netscape Navigator Internet browser would incorporate Java technology.

Now, nearing its twelfth year, the Java platform has attracted over 5 million software developers, worldwide use in every major industry segment, and a presence in a wide range of devices, computers, and networks of any programming technology.

In fact, its versatility, efficiency, platform portability, and security have made it the ideal technology for network computing, so that today, Java powers more than 4.5 billion devices:

  • over 800 million PCs
  • over 1.5 billion mobile phones and other handheld devices (source: Ovum)
  • 2.2 billion smart cards
  • plus set-top boxes, printers, web cams, games, car navigation systems, lottery terminals, medical devices, parking payment stations, etc.

Today, you can find Java technology in networks and devices that range from the Internet and scientific supercomputers to laptops and cell phones, from Wall Street market simulators to home game players and credit cards -- just about everywhere.

The best way to preview these applications is to explore java.com, the ultimate marketplace, showcase, and central information resource for businesses, consumers, and software developers who use Java technology.

Why Software Developers Choose Java Technology

The Java programming language has been thoroughly refined, extended, tested, and proven by an active community of over five million software developers.

Mature, extremely robust, and surprisingly versatile Java technology has become invaluable in allowing developers to:

  • Write software on one platform and run it on practically any other platform
  • Create programs to run within a web browser and web services
  • Develop server-side applications for online forums, stores, polls, HTML forms processing, and more
  • Combine Java technology-based applications or services to create highly customized applications or services
  • Write powerful and efficient applications for mobile phones, remote processors, low-cost consumer products, and practically any device with a digital heartbeat

Source: http://www.java.com/en/about/

What if you do not provide the string array as the argument to the method?

Actually speaking - Nothing. The Java program will start running. The args is just an optional parameter and you can pass it if you want and ignore it if you dont want to pass any runtime arguments

How do you use Graph in data structure?

Em depends on the points your plotting on the graph. If they are evenly distributed then it's probably a linear graph.

What is bytecode and java virtual machine in java?

A traditional compiler compiles the source code to machine language. Java compiles for a "ficticious CPU", not for a specific CPU (processor). The compiled code is called "bytecode" (technically, any information stored on a computer is made up of bytes!). To run the program, this bytecode is interpreted by the Java Virtual Machine.

Is there any difference between an inanimate object and a dead object?

In some ways this is a trick question as many dead objects, such as road kill, are themselves full of bacteria and viruses and larvae, so the are far from dead. In this circumstance we use the term dead to indicate the loss of a certain level of consciousness through the death of a multicellular creatures coordination system.

Living and dead is a concept, not a scientifically or logically provable state. The concept is that living objects are composed of nonliving matter and an organised living component. Dead objects are either past living objects or never have been alive, either way they are somehow missing the living component.

The simplest component is a nonliving, or never have lived, item such as a hydrogen atom. This is a theoretical object, as we can not presently measure or even observe the difference between this item in a living and nonliving state.

At present it has been measured that living complex microscopic structures utilize energy in a manner that can not be explained using the quantum or billiard ball style of physics modeling. The observed and measured energy production process within the mitochondria of all living cells is explicable only if we consider energy transfer as a wave or field.

It is therefore proposed that we presently place the division between living and nonliving objects as "living objects utilize energy as a field to power their coordinated chemical and electrical activities". Nonliving objects or components can be brought into this field or taken out. If this was the model adopted one can quickly see that we are starting to consider the 'object' as a nonliving component under the control of a field. This is also unlikely as modern physics considers atoms to be part of (entangled with) this field.

So we go full circle and perhaps have to conclude there is no such separation. That all things are, and always will be alive, but that the field is just more obviously, visually and energetically active in objects or structure we consider living.

Why method of java must be implemented in the class definition itself?

I think that the reason this question has not been answered is because nobody understands the question!!! What exactly are you asking???

So, I will try to answer the question, but only in a very general sort of way.

Let's create an example class called "Jonny."

The bear minimum of creating the class Jonny would be the following:

public class Jonny{

}

This text would, of course, need to be saved in a file named Jonny.java, and would be compiled into the Jonny class.

Now, if we are going to get into methods, lets start with a simple constructor:

public class Jonny

{

public Jonny()

{

}

}

Now, to the slightly trained eye, this statement is basically the same as the original without the constructor, because Java automatically provides you with a default empty constructor.

The great thing about constructors, however, is that we can use them to allow fast initializations of Objects (Which every object extends). So we can allow the user of our new class to create a Jonny with other parameters as well.

Let's make a Jonny extend JFrame, and then allow the user to initialize the frames name within the constructor:

public class Jonny extends JFrame{

public Jonny(String frameName)

{

this.setTitle(frameName);

}

}

Wow, that wasn't too hard was it.

I suspect that this is the method you were speaking of when asking this question, so the answer is that it is not really a "method," but is actually a CONSTRUCTOR, which creates the object itself. We can create actual methods as well within our new class, though:

public class Jonny extends JFrame{

public Jonny(String frameName)

{

this.setTitle(frameName);

}

public void showTheThingAlready()

{

this.pack();

this.setVisible(true);

}

}

So there you have it. The answer is that it is not a method, but a constructor. It is not required to explicitly have a constructor, but they can be very useful when dealing with new objects. I often allow myself to have several different constructors, allowing me different ways to create objects and initiallize them at the same time. It is a fairly common practice in Java to have many methods named the same thing as well, but with different types of input parameters.

I hope I have answered your question.

James