Java Tutorials

Change font size in Eclipse

Hey friends,

Many students & professionals were asking that how they can change the font size in Eclipse.

 

Here is the way to change the font size in Eclipse :

  • Go to Window Menu Preferences > General > Appearance > Colors and Fonts
  • Then to change the font size : Go to Java > Java Editor Text Font > Edit
  • Now set the font & font size you wish, done.

 

See Video Tutorial for above :

[gigya src=”http://player.videofy.me/player.swf?videoId=605346″ width=”480″ height=”256″ quality=”high” wmode=”transparent” allowFullScreen=”true”]

Enjoy Coding!

Hello World in different languages

Free Source Code

Free Source Code

Objectives :
* How to write Hello World in Java?
* How to write Hello World in C?
* How to write Hello World in PHP?
* How to write Hello World in C++?
* How to write Hello World in JavaScript?
* How to write Hello World in HTML?
* How to write Hello World in CSS?
* How to write Hello World in COBOL?

Java

[sourcecode language=”java”]
class HelloWorld
{
public static void main(String args[])
{
System.out.println(“Hello World!”);
}
}
[/sourcecode]

C

[sourcecode language=”c”]
#include
#include
void main()
{
clrscr();
printf(“Hello World!”);
getch();
}
[/sourcecode]

C++

[sourcecode language=”cpp”]
#include
#include
void main()
{
clrscr();
cout<getch();
}
[/sourcecode]

CSS

[sourcecode language=”css”]
body:before
{
content: “Hello World”;
}
[/sourcecode]

COBOL

[sourcecode language=”text”]
****************************
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
MAIN SECTION.
DISPLAY “Hello World!”
STOP RUN
****************************
[/sourcecode]

HTML

[sourcecode language=”html”]
<html>
<head>
<title>Hello World Example</title>
</head>
<body>
Hello World!
</body>
</html>
[/sourcecode]

JavaScript

[sourcecode language=”javascript”]
document.write("Hello World");
[/sourcecode]

PHP

[sourcecode language=”php”]
<!–?php echo ‘Hello World!’ ?–>
[/sourcecode]

Linux Shell Script

[sourcecode language=”text”]
echo "Hello World !"
[/sourcecode]

Enjoy coding!

Posted from WordPress for Android

Get MAC Address using Java

Free Source Code

Free Source Code

Objective :

* How to get the MAC Address of a computer?
* How to get the MAC Address of a computer using Java?
* How to get the MAC Address of a computer via programming?

[sourcecode language=”java”]
import java.net.*;

class GetMac
{
public static void main(String arg[])
{
try
{
InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
[/sourcecode]

Note : This code will return only the first net address; if you need other detils too, get a list.

Posted from WordPress for Android

Why Java is Platform Independent?

Objectives :

  • Why Java is platform independent?
  • How java program executes?
  • What is execution flow of Java Program?
  • What are the steps to execute a Java Program?
  • What are the steps to run a Java Program?

[gigya src=”http://player.videofy.me/player.swf?videoId=424278″ width=”480″ height=”300″ quality=”high” wmode=”transparent” allowFullScreen=”true” ]

Architecture of Java Program

Objective :

  • What is architecture of Java program?
Architecture of Java Program

Architecture of Java Program

While writing a java program, the above architecture need to be followed.

As the figure describes, documentation part or writing comments into program is completely optional. If you want to know, how to write comments & documentation in java? Click here

If the programmer wants to keep class in some package then only he/she will declare the package.

If we need to link any other class from some package then, we will write import statement otherwise if the classes are in same package there is no need of import statement.

Whereas class definition & program body is compulsory which will have your actual code.

Data Types in Java

In Java there are total 8 data types available which are further divided into 4 groups viz. integers, floating point numbers, character & boolean values.

Type

Size in Bytes (Bits)

Range

Default value

Contains

Integers
Byte 1 byte (8 bits) -128 to 127 0 Signed Integer
Short 2 bytes

(16 bits)

-32768 to 32767 0 Signed Integer
Int 4 bytes

(32 bits)

-2147483648 to 2147483647 0 Signed Integer
Long 8 bytes

(64 bits)

-9223372036854775808 to 9223372036854775807 0 Signed Integer
Floating point numbers
Float 4 bytes

(32 bits)

-3.4e38 to 3.4e38

(i.e. 3.4*1017)

0.0 IEEE754 Floating point number
Double 8 bytes

(64 bits)

-1.7e308 to 1.7e308

(i.e. 1.7*10151)

0.0 IEEE754 Floating point number
Characters
char 2 bytes

(16 bits)

\u0000 to \uFFFF \u0000 Unicode Characters
Boolean values
boolean 1 bit Not Applicable False true or false

Table : Data Types in Java

 

How to set Path for Java

Dear Friends,

If you want to set the path for Java, there are two main types for setting system path for Java which are as follows;

  1. Temporary Path
  2. Permanent Path

For the above two types, the video describes the complete step by step procedure for setting path for Java :

[youtube=http://www.youtube.com/watch?v=rM4Qq3Ua7Sk]

Or you can go through this tutorial :

1. To check path is set or not :
a. Go to Start Menu
b. Go to Run
c. Type “cmd” without quotes to open Command prompt.
d. Get to the root directory say c:/>
e. To check whether the path is already set or not, on command prompt type “javac” without quotes & press enter/return. If the following message appears on the console screen then the path is not set.

f. But if the other message having length nearly equal to half a page then path is set.

2. Temporary Path :

To set the temporary path follow the steps given below;
a. Open Command Prompt.
b. If path is not set &  want to set it temporarily, assume the Java 1.6.0 is installed on your system, type following command on console screen :
And finally the path is set temporarily.
Note : This will work only for the current window, won’t work if you open another window.

3. Permanent Path :

To set the path permanently follow the steps given below;
a. Right Click on “My Computer Icon”.
b. Go to “Properties”.
c. Go to “Advanced” tab.
d. Click on “Environment Variables”.
e. Go to “System Variables”.
f. Find “Path” variable, select it & click “Edit”.
g. Small window will appear. Go to end by simply pressing “End” button & type following path with semicolon ( ; ) as prefix but without quotes;
    “;C:\Program Files\Java\jdk1.6.0\bin
h. Press “OK”, again “OK”, again “OK”.
And finally the path is set permanently.
i. To check whether the path is set or not simply refer details given above in the section 1.

Writing Comments in Java

Objective :

  • How to write comments in Java?
  • How to write single line comment in Java?
  • How to write multi line comment in Java?
  • How to write documentation comment in Java?
  • What are the types of comments in Java?

Writing Comments in Java :

  • Comments are the statements which are never executed (i.e. non-executable statements).
  • Comments are often used to add notes between source code so that it becomes easy to understand & explain the function or operation of the corresponding part of source code.
  • Java Compiler doesn’t read comments; comments are simply ignored during compilation.
  • There are 3 types of comments available in Java as follows;
  1. Single Line Comment
  2. Multi Line Comment
  3. Documentation Comment

Single Line Comment :

This comment is used whenever we need to write anything in single line.

Syntax : 

//<write comment here>

Example :

[sourcecode language=”java”]
//This is Single Line Comment.
[/sourcecode]

Multi Line Comment :

This type of comments are used whenever we want to write detailed notes (i.e. more than one line or in multiple lines)related to source code.

Syntax :

/*

<write comment here>

*/

Example :  

[sourcecode language=”java”]
/*
This
Is
The
Multi
Line
Comment
*/
[/sourcecode]

Documentation Comment :

  • The documentation comment is used commonly to produce the javadoc for the respective program.
  • The javadoc is generally HTML, if used in project it is a set of multiple HTML files describing each java program in the corresponding project.
  • In the documentation comment we can add different notations such as author of the project or program, version, parameters required, information on results in return if any, etc.
  • To add these notations, we have ‘@’ operator.  We just need to write the required notation along with the ‘@’ operator.
  • Some javadoc tags are;
    1. @author – To describe the author of the project.
    2. @version – To describe the version of the project.
    3. @param – To explain the parameters required to perform respective operation.
    4. @return – To explain the return type of the project.

Syntax :

/**

*<write comment/description here>

*@author <write author name>

*@version <write version here>

*@param <write parameter here>

*@return <write return type here>

*/

Example :  

[sourcecode language=”java”]
/**
* This is Documentation Comment.
* @author Atul Palandurkar
* @version 1.0.0
*/
[/sourcecode]