Thursday, 9 August 2018

Java Concepts-1:


Java program structure:
----------------------------
Hierarchy in Eclipse:
-------------------------
Workspace (For organizing all your projects at one place)
  --> Java project (A collection of Packages)
--> package (A collection of Classes)
     --> class (A collection of Methods)
  --> Methods
---------------------------------------------------------------------------
Package:
Pacakge organizes a set of classes

How to create a "Package"
Navigation: {To create package}
Select "src" folder under java project in eclipse
Right click and Select "New" then Select "Package"
    Enter a "package name"
Click on "OK" Button

NOTE: Package name must be lower case ONLY
Exp Packages Names:  sample.test, selenium.testcases, org.insightq

---------------------------------------------------------------------------
Class:
Class is a Template using which we can write java programs

How to create a "Class"
Navigation:{To create class}
Select the "Package name" under "src" folder under java project in eclipse
Right click and Select "New" then Select "Class"
    Enter a "Class name"
Click on Check Box "Public Static Void Main (String []args)"
    Click on "Finish" Button

NOTE: Package name must be Camel case ONLY
Exp Packages Names:  SampleTest, SeleniumTestcases, OrgInsightq

Class with main method only
--------------------------
The below structure generated in edit box automatically by doing the above

package packageName;

public class ClassName
{
public static void main(String[] args)
{
---------
---------
}
}
---------------------------------------------------------------------------
How To Make "Comments" in Java:
-------------------------------

Single Line Comment:
Use two forward slashes "//" as prefix to comment a single line

Exp: //To validate login functionality

Multiple Lines Comment:
Use one forward slash and a star "/*" as prefix to open comment, */ as suffix to close comment

Exp: /*This is a
     block of code
for further use*/

Shortcut Keyboard Keys:
Ctrl+Shift+/ used to comment multiple lines
Ctrl+Shift+\ used to uncomment multiple lines
---------------------------------------------------------------------------
Output Functions:
-----------------

To display user messages during runtime, we can use following output functions

i. System.out.print(); It will print user message in console
syntax:
System.out.print(user msg);
Exp:
System.out.print("SeleniumClass1");
System.out.print("SeleniumClass2");

The output will be: SeleniumClass1SeleniumClass2

ii. System.out.println(); It will also print user message in console but line by line
syntax:
System.out.println(user meg);

Exp:
System.out.print("SeleniumClass1");
System.out.print("SeleniumClass2");

The output will be:
SeleniumClass1
SeleniumClass2

Shortcut Keyboard Keys:  //Syso+Ctrl+Spacebar+Enter

---------------------------------------------------------------------------
Scanner class:
--------------

To read user inputs during runtime (i.e. Keyboard inputs) we can use Scanner class
To use scanner class we need to create an obj in scanner class

syntax:

Scanner obj= new Scanner(System.in);
String variable=obj.nextLine(); //This object will provide methods to read data
obj.nextLine();
obj.nextInt();

Exp: Write program to read string value during runtime
System.out.println("Enter any name");
Scanner sc= new Scanner(System.in);
String myVal=sc.nextLine();
System.out.println("Entered value is: "+myVal);


Ex: Write program to read integer value during runtime
System.out.println("Enter any number");
Scanner sc= new Scanner(System.in);
int myVal=sc.nextInt();
System.out.println("Entered number is: "+myVal);

========================================================================



No comments:

Post a Comment