Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
83 views

Selenium With Java1-Introduction

The document provides an overview of the Java programming language including its history, features, requirements for use, and basic concepts like classes, objects, methods, and variables. It also demonstrates how to write a simple Java program, compile and run it, and create objects using constructors.

Uploaded by

Mallikarjun Rao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Selenium With Java1-Introduction

The document provides an overview of the Java programming language including its history, features, requirements for use, and basic concepts like classes, objects, methods, and variables. It also demonstrates how to write a simple Java program, compile and run it, and create objects using constructors.

Uploaded by

Mallikarjun Rao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Selenium with Java Courseware

Java is a high-level programming language originally developed by Sun Microsystems and released in
1995.
Oracle purchased Sun Microsystems
Java runs on a different platforms ex: Windows, Mac OS, and the various versions of UNIX.
The latest release of the Java Standard Edition is Java SE 8.
With the advancement of Java and its widespread popularity
 Multiple configurations were built to suit various types of platforms.
 J2EE for Enterprise Applications, J2ME for Mobile Applications.
Java is guaranteed to be Write Once, Run Anywhere.

Java is −

 Object Oriented − In Java, everything is an Object.

 Platform Independent − When Java is compiled, it is not compiled into platform specific
machine, rather into platform independent byte code.

This byte code is interpreted by the Virtual Machine (JVM) on whichever platform it is being run
on.

 Simple − Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it
would be easy to master.

 Secure − With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.

 Architecture-neutral − Java compiler generates an architecture-neutral object file format, which


makes the compiled code executable on many processors, with the presence of Java runtime
system.

 Portable − Being architecture-neutral and having no implementation dependent aspects of the


specification makes Java portable.
Compiler in Java is written in C, which is a POSIX subset.

 Robust − Java makes an effort to eliminate error prone situations by emphasizing mainly on
compile time error checking and runtime checking.

 Multithreaded − it is possible to write programs that can perform many tasks simultaneously.

This feature allows the developers to write applications that can run smoothly.

 Interpreted − Java byte code is translated to native machine instructions and is not stored
anywhere.

 High Performance − With the use of Just-In-Time compilers, Java enables high performance.

 Distributed − Java is designed for the distributed environment of the internet.

 Dynamic − Java is considered to be more dynamic than C or C++ since it is designed to adapt to
an evolving environment.

Java programs can carry extensive amount of run-time information that can be used to verify and
resolve accesses to objects on run-time.

In 2006, Sun released Java as free and open source software under the terms of the GNU General Public
License (GPL).

For writing Java Programs, you will need a minimum of 200-MHz computer with a 64 MB of RAM (128
MB of RAM recommended) and the software’s required are

 Linux 7.1 or Windows XP/7/8 or any versions of operating systems


 Java development Kit (JDK 1.x.x/2)
 Notepad or any other text editor

Download and set up Java on your machine, based on version of your operating system.

Install Java on your machine.

Once you installed Java on your machine, set environment variables to point to correct installation
directories.
Set Up the Path for Windows
Assuming you have installed Java in c:\Program Files\java\jdkdirectory −

 Right-click on 'My Computer' and select 'Properties'.

 Click the 'Environment variables' button under the 'Advanced' tab.

 Now, alter the 'Path' variable so that it also contains the path to the Java executable.

Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read
'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.

To write Java programs, you will need a text editor.

There are sophisticated IDEs available.

Consider one of the following −

 Notepad − On Windows machine, you can use simple text editor like Notepad

 Netbeans − A Java IDE that is open-source

 Eclipse − A Java IDE developed by the eclipse open-source community and can be downloaded
from https://www.eclipse.org/.

A Java program can be defined as a collection of objects that communicate by invoking each other's
methods.

What are class, object, methods, and instance variables

 Object − Objects have states and behaviors.

o Example: A car has states - color, name, brand as well as behavior such as Start, Stop.

o An object is an instance of a class.

 Class − A class can be defined as a template/blueprint that describes the behavior/state that the
object of its type supports.

 Methods − A method is basically a behavior.

A class can contain many methods.

It is in methods where the logic or code is written, data is manipulated and all the actions are
executed.
 Instance Variables − Each object has its unique set of instance variables.

An object's state is created by the values assigned to these instance variables.

Open notepad and write the code as follows

public class MyFirstProgram

public static void main(String [ ] )

System.out.println(“Welcome to Java”); // Prints Welcome to Java

Save the above program as MyFirstProgram.java

Open a command prompt window and go to the directory where you saved the class.

Assume it's C:\.

AT c :> javac MyFirstProgram.java

and press enter to compile your code.

If there are no errors in your code, the command prompt will take you to the next line If you have set the
path variable correctly.

To test the path variable, type

At C:> Java, it shows the java options

At c:> Java MyFirstProgram // to run your program.

You will be able to see 'Welcome to Java' printed on the window.


Keep in mind the following

 Java is case sensitive

o Welcome and welcome have different meaning in Java.

 For all class names the first letter should be in Upper Case.

Example: class MyFirstProgram

 All method/function names should start with a Lower Case letter.

Example: public void myMethodName()

 Name of the program should exactly match the class name.

Example: Assume 'MyFirstProgram' is the class name. Then the file should be saved
as 'MyFirstProgram.java'

 Java program processing starts from the main() method which is a mandatory part of every Java
program.

Types of variables in Java

 Local Variables
 Class Variables are Static Variables
 Instance Variables are Non-static Variables

A local variable in Java is a variable that's declared within the body of a method. Then you can use
the variable only within that method. Other methods in the class aren't even aware that the variable exists.

Class variable only have one copy that is shared by all the different objects of a class. A class variable is
a variable defined in a class of which a single copy exists, regardless of how many instances of the
class exist.

An instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated
object of the class has a separate copy, or instance. An instance variable is similar to a class variable.
 Instance variables are used by Objects to store their states.
 Variables which are defined without the STATIC keyword and are outside any method
declaration are object specific and are known as instance variables.
 They are called because their values are instance specific and are not shared among instances.

Comments in Java
 Single line comment is given as //
 Multiple Line comments given as
/*…….
/
/*

Constructors

Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler
builds a default constructor for that class.

Each time a new object is created, at least one constructor will be invoked. The constructor should have
the same name as the class. A class can have more than one constructor.

Object Creation

An object is created from a class.

In Java, the new keyword is used to create objects.

There are 3 steps when creating an object from a class −

 Declaration − A variable declaration with a variable name with an object type.

 Instantiation − the 'new' keyword is used to create the object.

 Initialization − the 'new' keyword is followed by a call to a constructor. This call initializes the new
object.
Example of Constructor and Object

public class MyFirstProgram

public MyFirstProgram(String name) // Constructor having one parameter name

System.out.println(“Invitation:” + name);

public static void main(String args[ ] )

MyFirstProgram myObject = new MyFirstProgram(“Welcome to Java”);

Compile and run MyFirstProgram

C:> Javac MyFirstProgram.java

C:> Java MyFirstProgram

Invitation : Welcome to Java

You might also like