Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Tuesday 29 September 2015

Use of Variables in JAVA

No comments :
variables-in-java
Variables:
Variable gives the name for the value which is a fundamental unit of storage in JAVA. In memory the values (like 20, 30, “hai”) are stored in a particular location, These values can be referred using variable names. We can assign different values to the variables, We can also change values of the variables at run time.
Example:
int a=10;
a=a+10;
System.out.println(“The value increased by 10”+a);
In the above line ‘a’ is the variable name and assigned to the value 10.
Then it is incremented by 10, after the second line of this code the value of variable a is changed from 10 to 20. So variables can be changed at runtime.
Variable name can be any word in the normal English.
The variables can be in the form of
int india;
int b_tech;
int _network;
int pop_nv;
char count;
It can be combination of alphabets or numbers or underscore, we can also create lengthy variable names but using of large unrelated names are not necessary.
The variables can’t be in the form of
int b sc;
char %hello;
int 0india;
double 2333;
char b-tech;
JAVA does not allow any numbers or special characters other than underscore or blank spaces in variable name.
Program:
import java.io.*;
class sample
{
public static void main(String a[])
{
int number;
double val;
char _letter;
try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Any Number");
number=Integer.parseInt(br.readLine());
System.out.println("Enter Any Decimal Point Number");
val=Double.parseDouble(br.readLine());
System.out.println("Enter Any Character");
_letter=(char)br.read();
System.out.println("Number : "+number);
System.out.println("value : "+val);
System.out.println("character : "+_letter);
}
catch(IOException e)
{
}
}
}
Different types of variables are declared in this program, Integer (For whole numbers), Double (For decimal point numbers), Character (For single alphabet) are different types of datatypes in Java programming that will be explained in next tutorial. Just concentrate on the names of the variables and scanning & displaying of variables.
In this code while scanning the variable _letter (character type) there is no parsing function because Character is also like String so there is no need to parse it.
“System.out.println("Enter Any Character");
_letter=(char)br.read();”
This example code may consist of some blocks like try, catch which are under the topic of exception handling that was important aspect of JAVA programming, for including BufferedReader class this blocks must present to execute program without errors.
Types of Variables:
In JAVA variables are classified into three types based on the usage and accessing of variables.
1. Local Variables
2. Instance Variables
3. Class or Static Variables
Local Variables:
Local Variables are declared inside methods and constructors.
There is no access modifier is assigned to the local variables.
The local variables should be initially assigned to a value.
like int a=0;
After that we can change this value and print it
int a=0;
a=a+7;
System.out.println(“The value of a: “+a);
Instance Variables:
Instance Variables are declared inside the class and outside the methods or constructors (Methods and constructors are main key terms used inside the class will be discussed later).
In JAVA access modifiers are provides the limit to the others (Like calling a variable outside the class or call variable from other program) we can prevent others access of our data or variables. In Instance variables we can provide access modifiers to the variables.
Example:
public int a;
private char k;
Class or Static Variables:
Class or Static Variables are declared with static keyword inside the class outside the methods or constructors
These static variables can be called using the class name.
Example;
static int i=889;
Note: Concepts like Calling of variables using class name, methods, constructors, Access modifiers will be explained in coming tutorials, now only consider the variable names and their types (Only consider that the In JAVA three types of variables are used and declaring these variables can differ).
Example (Based on types of variables):
import java.io.*;
class vardemo
{
int a=10;/* Instance Variable-declared inside the class outside the method*/
static int b=20;/*Static Variable with static keyboard*/
void method()
{
int c=30;/*Local Variable declared inside the method*/
}
}
This example is a piece of code that represents the concept of variables and methods are similar to that of functions in C. We will learn about the methods later.
Thus the importance of variables is explained in this post, i hope it was useful to you and gain some information about variables in java programming.
In the upcoming tutorials we can learn about methods, access specifier, etc..





























































































Read More

Monday 28 September 2015

What is public static void main() in JAVA

No comments :
public-static-void-main
Since we have already read about basic Java programs in previous posts and we have seen that a line public static void main(String args[]).
So what is the use of it ? and why we are using it? Lets see about it in detail.
This statement is an important statement that will be present in all JAVA programs.
Even if a program contains many classes then the class which contains this public static void main(String args[]) is considered as main class.
This is the most important compulsory statement in all JAVA programs. The program execution will start from this method only.
To know the meaning of this statement first we just want to know the meaning of each of the word in this line.
First “public”
Public is the visibility mode that was assigned to the method “main()”. Hence it is declared as public so anyone can be allowed to access this method.
main() method must be declared as public, because it must be called when program starts. Various types of visibility modes will discussed in the next tutorials.
“static”
Static is a keyword. This keyword allows the programmer to call the function by using the class name rather than using object name. In JAVA programming language the normal method for calling method and variables can be done using objects but this static keyword reduces the creation of objects for calling them, Because “main()” method is called by the operating system, in operating system there is no such environment for calling methods using object creation, so that JAVA provides effective way of calling methods by the rest of object creation.
In other words Static is a keyword which is used in front of a class or method so that the method or class cannot be changed. So that it is defined for some specified sort of work.

“void”
void is the return type for the method “main()”. Hence main method is declared as void, it does not return anything.
“main(String args[])”
“main()” is the name of the method, and String is the type of the parameter args. This args can be any other words such as arg . Because of this in JAVA programming language all types of inputs are taken as String format. So if we want to do some process on other data types such as integer , float we have to convert it into string type.
Thus the statement public static void main(String args[]) is very important.

















Read More

Tuesday 22 September 2015

How to get Input from the user in JAVA

1 comment :
JAVA
   The programs should interact with the user and should provide services for users (For taking the addition program as example user only need to give values to add then program will automatically calculate and print the result).
   In Java there are various methods to get the input from the user.
For getting input from the user we need scanner functions to scan the input and perform operation.
Java provides many functions and methods for scanning the inputs from the user. Each of them is specific and different.
The first method is to use BufferedReader class. It is a simple and basic method used for getting input and store in a variable.
Just read the following example:
 
import java.io.*;
class add
{
public static void main(String args[])
{
int a, b, c;
System.out.println(“Enter the values of A and B
to add:”);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
c=a+b;
System.out.println(“The result c=”+c);
}
}
 
This program allows user to enter different values to add.
For getting input or scanning the input from the user first create an object for BufferedReader class (br) and InputStreamReader (in) class, by the statement
“BufferedReader br=new BufferedReader(new InputStreamReader(System.in));”
(Object creation is a very basic and essential thing in JAVA programming language. Object is created for many purposes, in this program for scanning purpose two objects are created.)
After that we have to convert the user inputs to the required datatypes, because java language takes any value as a string (Set of characters), we have to convert that string to required datatypes using the following statements.
“ a=Integer.parseInt(br.readLine());
  b=Integer.parseInt(br.readLine()); ”
In the above statement Integer is a class, parseInt is a method to convert the integer type data into String type data.
Because java only supports string data types we have to convert the other data types into String, thus we use this conversion method.
  Thus normal addition operation is performed by getting input from the user.
Now compile and run this program so that  getting input from the user and performing addition operation is performed.
See here how to compile and run the Java program.
How to run java program






































Read More

Monday 21 September 2015

Addition of Two numbers in Java

No comments :
add
     Addition of two numbers is a basic program like hello world in JAVA.
There are several methods used to add two numbers in JAVA.
The very basic concept used to add two numbers was given below:-
import java.io.*;
class add
{
public static void main(String args[])
{
int a=10;
int b=20;
int c;
c=a+b;
System.out.println(“Addition of two numbers=”+c);
}
}

Program Explanation

In this program the first line “import java.io.*” is the header file (contains package name) that provides the related classes and methods for executing the program.
A class name can be explicitly specified in place of “*”.
Then the class name is “add’.
Inside the class main method is defined. Inside the method Integer variables are declared and values are assigned (int a=10, int b=20).
Another Integer variable is declared to store the result value.
Then in the operation part using “+” operator Addition operation is performed and the result is stored in “c”.
Finally the result is displayed from the variable c using the statement “System.out.println(“Addition of two numbers=”+c);”

System.out.println(“”);

In this statement System is a predefined class and out is the static member of the system class and println is the method of print stream class.
Anything mentioned between double quotes inside this System.out.println will displayed in the output screen.
If we want to print the value of any variable that was mentioned after the “+” from double quotes. For example see the above highlighted code.
In this statement the first letter S must be in Uppercase.  
Because JAVA is a case sensitive language and the predefined classes (Like System, String etc.) in JAVA classes must be start with uppercase.
Then compile the program and execute it.
That’s it we have done. We have successfully added two numbers in JAVA.
















Read More

Monday 31 August 2015

What is JVM (Java Virtual Machine)

No comments :
JVM-BY NAREN
Many of them work with Java but they do not known what is a JVM which is an important to be known as a Java Programmer. So let we see in detail about What is a JVM is with a detailed discussion .
 
To execute any program written in any programming language on a microprocessor it has to be converted into machine language instructions understood by that microprocessor. This process is known compilation.

Machine language instructions of different microprocessor are different. Thus instructions of an Intel microprocessor is different than an ARM microprocessor.

Therefore any program being executed on a specific microprocessor needs to be  converted into machine language instructions that the microprocessor understands.

The machine language instructions for one microprocessor differs from one to another.

Hence if a program is compiled for one microprocessor it may not work on the another
microprocessor it would have to be compiled for that microprocessor again.

compilation-in-c 

This is a “Write once, Compile anywhere” scenario. It means to make the same program work on different microprocessor we are not required to rewrite the program, but are required to recompile the program for different microprocessors.

But the Java is different in this approach

“ Compile once, run anywhere ” .

This implemented through  a program called JAVA VIRTUAL MACHINE (JVM) .

When we compile Java Programs they are not converted into machine language instructions for a specific microprocessor, but a bytecode instructions are similar to machine code, but are intended to be interpreted by JVM. A JVM provides an environment in which JAVA BYTECODE can be executed.

Different JVMs are written specifically for different host hardware and operating systems.

For example, there is JVM for Windows and different JVM for Linux.

A JVM is distributed along with a set of standard class libraries that implement the Java Application Programming Interface (API) .

The JAVA APIs and JVM together form the JAVA RUNTIME ENVIRONMENT (JRE).
All these components of JAVA platform are shown in the below diagram

java-platform

  During execution the JVM runtime executes the bytecode by interpreting it or compiling it using just-in-time compiler (JIT) .

JIT Compilers are preferred as they work faster than interpreters.

During interpretation JIT compilation the bytecode is converted into machine language instructions for the microprocessor on which the program is being executed.

The java program is stored in .java file. The bytecode is usually stored in a .class file. A complex program may consists of many .class files .

For easier distribution these multiple class files may be packaged together in a .jar file ( java archive file ).

jre&jvm

This is how the JVM is used

detailed-jvm

I think you have understood what is a JVM is.
Keep visiting…. Thank you…..
Related Post: How to run a Java Program
Read More

Tuesday 4 August 2015

HOW TO RESOLVE ‘javac is not recognized as an internal or external command’ error in Java

No comments :

javac is not recognized as an internal or external command’

When you run the java program for the first time on your computer the error message ‘javac is not recognized as an internal or external command’ error in Java  will be displayed because you have not set a complier to your program.

JAVAC is the compiler we use to compile the java program.

So how to add the javac to your program…

here is the tip..

Right click on the computer in the Start menu and click Properties.

My-Computer

then the below window will be opened…

In that click Advanced System Settings option…

AdvancedSystemSettings

Then a window will be opened in that click the ENVIRONMEN VARIABLE

Environment-Variable

 

Then the below will be opened

Environment-Variables

In that look for the option PATH IN THE SYSTEM VARIABLES

Path

Suppose if no path is found you can add it by clicking on New option.

then you have to set the path of your compiler here..

So open your jdk where you have installed

C

Program-Files

Open the JDK

jdk

Then Click on BIN

Bin

Then after opening the Bin copy the path by clicking the mouse on the above path

Copy-the-path

Now come to the System variable in Environment Variable

and paste the path that you have copied in the PATH and click semi-colon(for example

C:\Program Files\Java\jdk1.7.0_79\bin; )

set-path

Then click ok.

But one more thing is remained. That is setting up your CLASSPATH It is also in the SYSTEM VARIABLES if it is not found create it by clicking on New option.

Now it is time to set the path of library ‘lib’ found in the JAVA RUNTIME ENVIRONMENT ‘JRE’

JRE

LIB

Copy the path and paste it in the CLASSPATH in SYSTEM VARIABLES

Copy-the-path

new

CLASSPATH-CRATE

Type CLASSPATH in the Variable name: and paste the path that you have copied it in the Variable Value and click ok.

OK

Then click OK for Environment Variable and click ok for System Properties.

 

System-properties

Now you can confidently go and run the java program as I have posted in the last post “how to run a java program” if you have missed that click the below link to open to see How to run a Java program

http://www.millioninformations.com/2015/08/how-to-run-java-program.html

That’s it You have successfully resolved the error

javac is not recognized as an internal or external command…

If you have further queries comment below or mail me on narenblogger@gmail.com

Thank you for spending your valuable time to learn this thing…

Read More

Monday 3 August 2015

How to run a Java Program

No comments :

Java

 

Java the one and only programming language which is used in more than 300 billion devices.

James Goslin, Mike Sheridin, and Patrick Naughton initiated the Java Language Project in June 1991. Initially Java is named as OAK (Oak tree was outside the Goslin’s office) later it was named as Green then finally named as Java from Java coffee.Gosling designed Java with a C/C++-style syntax that system and application programmers would find familiar.

james-Goslin

At first Java was released in the year 1995 by the Sun Microsystems later in 2009-2010 Java was owned by the ORACLE corporation.

Java is platform independent it assured that Write Once Run Anywhere(WORA).

Let us start coding…..

In order to run the Java program we need an Java Developement Kit (JDK) .

Download the latest jdk

Open the browser and Download the JDK

jdk7

click-link

DOWNLOAD

Then install the JDK

DOUBLE-CLICK-TO-INSTALL

To check that the Java is installed on your computer open the command prompt and type JAVA and click Enter.

cmd

Ok you are done…

Open the Note Pad and type the following Java code

import java.io.*;

import java.lang.*;

public class demo

{

public static void main(String args[])

{

System.out.println(“This is my first java program”);

}

}

notepad

Then save it in a well-known destination path

The saving should be as classname.java in the above program the class name is demo.

So save the file as demo.java

classname.java

You should remember the path of saving because it is required for the next process.

To run the Program open the command prompt

cmd

cmd-opened

in that type the local disk drive letter that you have saved.

I have saved it in the E disk.

type E: the cmd

open-the-disk

Then change the directory(folders) to which you have saved the Java file using CD folder name command

change-directory

change-directory

i have saved the java file in the above path as demo.java

we need to compile the program.

type the following command in cmd

javac filename.java

here it is javac demo.java and click Enter

javac filename.java

If there is no error the next line for command will be displayed else the errors will be displayed

compiled

Then we are now ready to run the program type the following command in cmd

java filename

here it is java demo and click enter the output will be displayed.

output

hurray we got the output…

Congratulations You became the basic Java Developer…

Basically while compiling for the first time error will be occured. I will help you to recover the error in the next tutorial…

Stay tuned…….

Read More