0% found this document useful (0 votes)
85 views

P-Programming in Java - BSC - 3rd Sem - Unit-1

This document provides instructions for installing Java, executing Java code, understanding the structure of Java programs, accepting user input in Java using the Scanner class, and installing the Eclipse IDE. It discusses downloading and installing the Java Development Kit (JDK), setting the PATH and CLASSPATH environment variables, the basic structure of a Java class including the package, import, interface, class and main method, and how to compile and run a Java program from the command line or an IDE like Eclipse. It also covers different Scanner methods for accepting user input and the differences between next() and nextLine().

Uploaded by

Berlin Rj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

P-Programming in Java - BSC - 3rd Sem - Unit-1

This document provides instructions for installing Java, executing Java code, understanding the structure of Java programs, accepting user input in Java using the Scanner class, and installing the Eclipse IDE. It discusses downloading and installing the Java Development Kit (JDK), setting the PATH and CLASSPATH environment variables, the basic structure of a Java class including the package, import, interface, class and main method, and how to compile and run a Java program from the command line or an IDE like Eclipse. It also covers different Scanner methods for accepting user input and the differences between next() and nextLine().

Uploaded by

Berlin Rj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SRM Institute of Science and Technology

NCR Campus, Modinagar

Department of Computer Applications

Program: B.Sc. Computer Science


Course Name: Programming In Java
Course Code: USA20301J

Unit-1 (Practical)

Downloading and Installing

Java Code Execution

Java code can be executed using notepad and command prompt.

Java code can be executed using any IDE, Eclipse, NetBeans.

Java Program Structure

Data Input in java

Eclipse IDE
1. Downloading and Installing

● Go to http://www.oracle.com\technetwork\java\javase\downloads\index.html.
● Choose java platform standard edition link. [Latest JDK]
● Download the version that is appropriate for your OS.
● Follow the installation instructions.
● Set the PATH.

1.1. PATH and CLASSPATH in Java


● PATH and CLASSPATH are two most important environment variables of the Java
environment which is used to find JDK binaries used to compile and run Java in
windows and Linux and class files which compile Java byte codes.
● Following are the major differences between these two:
○ The path variable is set to provide a path for all java tools like java, javac,
javap, javah, jar, appletviewer.
○ classpath variable is set to provide a path of all java classes which is used in
our application.
○ The path points to the location of the jre i.e. the java binary files such as the
jvm and necessary libraries.
○ The classpath points to the classes you developed so that the jvm can find
them and load them when you run your product.
1.2. Directory
1.3. Commands to set CLASSPATH AND PATH variable
1.3.1. Temporary Setting:
Command to set PATH in Windows is
PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin
Command to set CLASSPATH in Windows
set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib

1.3.2. Permanent Setting:


This class path setting is temporary, once you close the command prompt, this setting will
be deleted. For permanent CLASSPATH or PATH setting use windows environment variable
dialog box:
Right click on my computer -> properties ->advanced system settings ->Environment
variables- >Classpath….<write path for bin folder>

2. Java Code Execution


● Java code can be executed using notepad and command prompt.
● Java code can be executed using any IDE, Eclipse, NetBeans.

2.1. Entering a program

● The first thing that you must learn about java is that the name you give to a source file is
very important.
● source file: It is a text file that contains one or more class definitions. The java compiler
requires that a source file use the .java filename extension.
● In java all code must reside inside a class.
● By convention, the name of that class and capitalization[Java is case sensitive] should
match the name of the file that holds the program.
● File name convention makes it easier to maintain and organize your program.
● Filename must be followed by .java extension.

2.2. Code Blocks

● Code blocks are defined using { }.


● In java every class declaration and method declaration must be enclosed in code blocks.
● Java fields and methods have either block or class scope.
● A block is executed by executing each statement from first to last [left to right ] written
within the block.
2.3. Compiling and Execution of Program

● Compile → javac filename.java


● The javac compiler creates a file called filename.class that contains the byte code
version of the program.
● byte code is the intermediate representation of your program that contains instructions
that JVM (Java Virtual Machine) will execute. Thus, the output of the javac is not code
that can be directly executed.
● To run the program you have to use a java application launcher , called (JVM). For
execution use: java classname

When source code is compiled , each individual class is put into its output file named
after the class and using the .class extension. when you write java filename.class , you
are actually specifying the name of the class that you want to execute. It will
automatically search for a file by that name that has the .class extension. If it finds the
file, It will execute the code contained in the specified class.

2.4. Rules

● Always start with class keyword


● After typing the program, save the file in the name of the class. ( ex: test . java)
● Source file name and class name should be the same(even case also).
● Extension must be .java
● Compile the file using javac compiler c:\>javac test.java
● Running we must use java interpreter c:\>java test

3. Java Program Structure

● Documentation (Suggested): it comprises a set of comment lines. comments


are preceded by two slashes (//) in a line, or enclosed between /* and */ in
one or multiple lines.
● Package (optional): the package defines where this class lives relative to
other classes and provides a level of access control
● Import (optional): Similar to #include statement. This keyword defines other
classes or groups of classes that you are using in your class. The import
statement helps to narrow what the compiler needs to look for when resolving
class names used in this class.
● Interface (optional): Like a class but includes only method declaration. It is a
Java way of implementing multiple inheritance.
● Class(Essential) : java programs can have any number of classes. It is an
essential part of the Java program. Class keyword precedes the name of the
class. The name of the class and name of the must match when the class is
prefixed with a public keyword. The keyword public before class keyword id
modifier and not essential.
● main(optional) : starting point

Example

class test
/* this is a first program */
{
public static void main (String ar[])
{
System.out.println(“welcome to the world of java”);
}
}

4. Data Input in java

There are various ways to accept input from user in java program:
● Using Scanner Class
● Using Streams
● Scanner class belongs to java.util package and has following methods to accept data:
○ Public String next() -> Finds and returns the next complete token from this
scanner.
○ Public boolean nextBoolean() -> Scans the next token of the input into a
boolean value and returns that value.
○ Public byte nextByte() -> Scans the next token of the input as a byte.
○ Public double nextDouble() -> Scans the next token of the input as a double.
○ Public float nextFloat() -> Scans the next token of the input as a float.
○ Public int nextInt() -> Scans the next token of the input as an int.
○ Public String nextLine() -> Advances this scanner past the current line and
returns the input that was skipped.
○ Public long nextLong() -> Scans the next token of the input as a long.
○ Public short nextShort() -> Scans the next token of the input as a short.

4.1. Difference between next and nextLine()

● next() can read the input only till the space. It can't read two words separated by
space. Also, next() places the cursor in the same line after reading the input.
● nextLine() reads input including space between the words (that is, it reads till the
end of line \n). Once the input is read, nextLine() positions the cursor in the next
line.

5. Eclipse IDE

Eclipse is a free open source platform, Integrated Development Environment (IDE)


with the help of which applications are made using the Java programming
languages and other programming languages are also used such as C/C++, PERL,
Python, Ruby, etc.

5.1. Steps:
Step 1: In the first step, Open your browser and navigate to this
https://www.eclipse.org/ .
Step 2: Then, click on the “Download” button to download Eclipse IDE.

Step 3: Now, click on the “Download x86_64” button.


Step 4: Then click on the “Download” button. After clicking on the download button
the .exe file for the eclipse will be downloaded.

Step 5: Now go to File Explorer and click on “Downloads” after that click on the
“eclipse-inst-jre-win64.exe” file to install Eclipse IDE.
Step 6: Then, click on “Eclipse IDE for Java Developers”.

Step 7: Then, click on the “Install” button.


Step 8: Now click on “Create a new Java project”

You might also like