Concatenation of string with number in java?
The concatenation can be done using the "+" operator. The output of this concatenation would be a String.
Ex:
public String concatenate(int val, String phrase) {
return val + phrase;
}
If you invoke this method with 10 and years as the arguments the ouput would be
10 years.
Use and importance of programming languages?
Answer If it weren't for computer languages where would we be today. Can you imagine writing a program as simply as writing a sentence instead of using computer language, it would be impossible. CD/delete means one thing, CD:/change direction and if the double dots aren't there the computer won't recognise the command. That's why so many people especially Computer Programmers spend years in Schools learning computer language so that the every day joe can't do their jobs.
Difference between function and method?
Constructor will be automatically invoked when an object is created whereas method has to be called explicitly. Constructor needs to have the same name as that of the class whereas functions need not be the same. * There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. * There is no return statement in the body of the constructor. * The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.
Can you initialize an object without constructor in java?
No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.
Quite simply, an inner class is one class within another. Typically the inner class will be a private inner class, and will only be used by the outer class.
class MyOuterClass {
class MyInnerClass {
}
}
What are decision statements in java?
Decision statements are statements that are used for taking logical decisions. They contain a condition whose output is a boolean true or false.
Ex:
if (name.equals("Rock")) {
System.out.println("Hi Rocky!!!");
}
The output of the if condition will depend on the value that the variable name holds. This is a decision statement
Main method in java is always static as main method id the only method from where the program execution starts,and as we all know that main method is defined inside a class so JVM needs to make a object of the class to call the method and objects are build inside the main method ,so to execute the main method it has to make objects of the class but objects are build inside main method so that's the reason that main method is static so that JVM can execute the main method without making its object as static members can be called by class anme only
In Java where do instance variables stored in memory?
An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.
An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.
An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.
An instance variable is part of an object. Therefore, it gets stored together with the object, on the heap. The heap is the part of memory which is used to store objects.
A java do while loop to count even numbers from one to ten?
The while loop is good for scenarios where you don't know how many times a block or statement should repeat, but you want to continue looping as long as some condition is true. A while statement looks like this:
while (expression) {
// do stuff
}
In this case, as in all loops, the expression (test) must evaluate to a boolean result. The body of the while loop will only execute if the expression (sometimes called the "condition") results in a value of true. Once inside the loop, the loop body will repeat until the condition is no longer met because it evaluates to false.
Any variables used in the expression of a while loop must be declared before the expression is evaluated. In other words, you can't say
while (int x = 2) { } // This is not legal
The key point to remember about a while loop is that it might not run at all. If the test expression is false the first time the while expression is checked, the loop body will be skipped and the program will begin executing at the first statement after the while loop. Look at the following example:
int x = 8;
while (x > 8) {
System.out.println("in the loop");
x = 10;
}
System.out.println("past the loop");
Running this code produces
past the loop
Because the expression (x > 8) evaluates to false, none of the code within the while loop ever executes.
What two steps are needed to create an array?
For example we have a class :
public class Test()
{
public void print()
{
System.out.println("Hello");
}
}
So, if you want to create array from Test type. You can try something like this :
Test[]myTestArray=new Test[2];
myTestArray[0]=new Test();
myTestArray[1]=new Test();
I hope it will help.
What are the advantages of PHP over Java?
HTML - displays data. That's it. That's all it can and will ever do.
as for PHP, I'll list a few things that it's capable of:
-database interaction (add, modify, delete data. Alter database structures and more)
-Output dynamic contents. (ie, do different things according to the time of day, number of time the user has logged in, number of files in a directory, entries in database, etc.). And it's not just text and numbers either. It can generated images, output PDF and more~
-string/text/date manipulation
-error checks
-sessions and cookies (where website remembers you for a period of time)
-compressions and archives
-Cryptography extensions
-maths
and MUCH, MUCH more.
Why do you need a constructor as a class member?
A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself.
The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class.
Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.
What is the purpose of transient keyword in Java?
A variable which is declared as transient will not be serialized. This means that when you use an ObjectOutputStream to store the current state of a class, anything labeled as transient will be skipped over.
A common use of this keyword is to ensure that sensitive user information (usernames, passwords, etc.) is not accidentally saved to a file.
How the equals operator differs from the equals equals operator?
obj2 = true only if both has same memory location.
on the other hand equals() is a method defined in Object class and has overridden in many classes including String, Integer and provides logical equality e.g. two strings are equal by equals() method if there content are same.
Java program to display factors of a number?
One solution is to use a for-loop counting from 1 to half of the number. If the modulus of the number and the current value in the loop is zero, the current value is a factor of the number. The number divided by the current value is also a factor.
What does java generate after compiling the java source code?
Java compiles to Java byte code; the native language of the Java virtual machine (JVM). The JVM is essentially just an interpreter for Java byte code. Each supported platform has its own JVM implementation so the same Java byte code can be executed upon any platform without further compilation, unlike C++ where source code must be compiled separately for each supported platform. However, interpretation results in slower execution speed and higher resource consumption than with C++ which compiles to native machine code.
What are different types of data called?
Some different types of data are real-valued, integer, or Boolean. Boolean Data is data that represents true or false statements Fixed point data types are convenient for representing monetary values
How do you copy a string from one method into another string in another method in java?
You can have two String variables (note that String variables are object references) refer to the same String object like so:
String str1 = "Hello";
String str2 = str1;
Now the str1 and str2 are references for the same String object containing the word "Hello".
If you actually want a new String object with a copy of the contents of the original String, you use the String constructor that takes a String argument, like so:
String str3 = new String(str1);
Now str1 and str3 refer to SEPARATE String objects that happen to contain the same sequence of characters (the word "Hello").
Since Strings objects in Java are immutable, they can be shared without worrying about the contents used by one variable being upset by the use through another variable as might happen with char[] arrays in C or C++ so the first method is probably sufficient for most cases.
Explain the term identifier in java?
Identifiers are the strings you use in Java source code to identify unique things, such as variables, classes, and methods. Identifiers may be any word beginning with a letter or underscore, and continuing with letters, numbers, and underscores. Identifiers are case sensitive.
Advantages of Exception handling in java?
Exception handling helps us catch or identify abnormal scenarios in our code and handle them appropriately instead of throwing up a random error on the front-end (User Interface) of the application.
Exception handling allows developers to detect errors easily without writing special code to test return values. Even better, it lets us keep exception-handling code cleanly separated from the exception-generating code. It also lets us use the same exception-handling code to deal with a range of possible exceptions.
In Java what is the difference between heavyweight and lightweight components?
The basic difference between Swing and AWT is that Swing APIs are purely Java libraries i.e. they don't at all depend on the native libraries to draw graphical components. Because of this feature they provide a consistent look and feel on all platforms. AWT libraries require the support of native graphics libraries and some of their GUI components look different on different platforms. Moreover, Swing components are not inherently Thread safe, you explicitly have to write synchronized code to manipulate or redraw them whereas AWT components can be trusted in a multithreaded environment. AWT Components are called heavyweight components because of their dependency on native libraries. Swing components are called lightweight due to their independence of native libraries. Hence Swing operations are much faster because each and every operation is taken care by the Java runtime env and no delegation of events or commands to the native libraries is required.
What is method declaration in Java?
A method declaration is the heading of a method containing the name of the method, its parameters, and its access level. The method heading in Java is organized as such:
[access keywords] [return type] [method name] ( [parameters separated by commas] )
for instance:
public String toString(); is public (accessible by any class), returns a String, is called toString, and takes no parameters.
Other features could be added to the method declaration for a more specialized method such as static (method could be called without an object of that class), native (implemented using the native code, usually what C has already done, i.e. square root, power etc.).
Why should main be declared static and is declaring it public and void not sufficient?
The static modifier means that it does not have to be instantiated to use it. Before a program runs there are technically no objects created yet, so the main method, which is the entry point for the application must be labeled static to tell the JVM that the method can be used without having first to create an instance of that class. Otherwise, it is the "Which came first, the chicken or the egg?" phenomenon. Your main method should be declared as follows: public static void main (String[] args) { lots of your java code... } As we know, java is a pure OOP , that means everything should be in the class, main. Aso, because main is itself a function, static member functions should not refer to objects of that class. But we can access static functions through classname itself, as: class TestMain { public static void main(String args[]) { body; } } Now, cmd>javac TestMain.java cmd>java TestMain as we know the static member functions has to call through its class name. That's why the programme name must be same as the class name ,where we wrote the main function. Another important point is : static variables or member functions will load during class. That means before creating any instances(objects), the main function is the first runnable function of any program which we run manually, such as : cmd>java TestMain(run) if , any sharing information.
void main()
{char a[30] = "India";
//Let the string be stored in a[30];
char b[30]; //declaring another string
for(i=0;i<strlen(a);i++)
{b[strlen(a)-1 - i]=a[i];}
//Now reverse string is stored in b;
//to print it
cout<<b;
getch();
}