You are on page 1of 13

1.

Create and close and delete java project

2. Create Java project --> create package under SRC (source) --> create class under package

2.a) While creating class 'method stubs' will be ''public static void main (String[]args)

Java

=====

Package --> Class --> method(method means a code which performs some action)

Whatever we code inside {} after the method will be executed


Comments (Example: // TODO Auto-generated method stub)

==========

comment says what is the purpose of this class or method.... It just comments something. It is only
documenting what you want to say about it. It is not executed.

How to make a comment

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

There are two methods for making a comment

First Method

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

'/*' and press enter

Second Method

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

'//' and write comment

See example below (comment inside '/*' and '*/')

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

Method One

-----------

/*

* this is my first project

*/
Method two

--------------

// this is my first project

Outline (Also teacher calls it by name 'perspective')

=========

Box on the right side of eclipse which gives index kind of view of class, method etc. If we click on this,
the curson will go directly to it.

### Java already has some inbuilt classes to help us program

### Most important class in Java is called 'System'

========

If we type 'System.' in Java it will show what are methods available in System class

Example: System.out.println("Hello World");} - This will give output as 'Hello World'


https://www.youtube.com/watch?v=r59xYe3Vyks&list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-Al

Variables and Types in Java

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

Names that we declare to store some value

https://www.learnjavaonline.org/en/Variables_and_Types

Although Java is object oriented, not all types are objects. It is built on top of basic variable types called
primitives.

Here is a list of all primitives in Java:

package MyPackage;

public class MyClass {

/*

byte (number, 1 byte)

short (number, 2 bytes)

int (number, 4 bytes)

long (number, 8 bytes)

float (float number, 4 bytes)

double (float number, 8 bytes)

char (a character, 2 bytes)

boolean (true or false, 1 byte)*/

public static void main(String[] args) {

short my_variable=10;
float my_decimal=(float)4.5;

double my_double=11.56;

char my_char='A';

boolean is_true=false;

System.out.println(my_variable);

System.out.println(my_decimal);

System.out.println(my_double);

System.out.println(my_char);

System.out.println(is_true);

}
https://www.youtube.com/watch?v=qgMH6jOOFOE&list=PLS1QulWo1RIbfTjQvTdj8Y6yy
q4R7g-Al&index=5

Getting User Input using Java


How can users give inputs?

package lesson1;

import java.util.Scanner;

public class MyClass {

public static void main(String[] args) {


Scanner scan = new Scanner (System.in);
System.out.println("Enter Some Number");
int user_input_number= scan.nextInt();

System.out.println("The Entered Value Is");


System.out.print(user_input_number);

Scanner scan1 = new Scanner (System.in);


System.out.println("Enter Some Decimal Value");
double user_input_double= scan.nextDouble();

System.out.println("The Entered Value Is");


System.out.print(user_input_double);

Scanner scan2 = new Scanner (System.in);


System.out.println("Enter Some String");
String user_input_string= scan2.nextLine();
System.out.println("The Entered String Is");
System.out.print(user_input_string);
}

What I learned?

Int – it means to give some number (example: 1222)

Double – it allows to give a decimal value (example: 122.4345)

String – it allows to add some text (example: hi there, how are you?)
https://www.youtube.com/watch?v=ss7BtLrbxp4&list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-Al&index=6

Math and Arithmetic Operators in Java


How to do basic Maths calculations in Java?

For Addition

package lesson1;

import java.util.Scanner;

public class MyClass {

public static void main(String[] args) {

int x, y, answer;
x = 20;
y = 30;
answer = x + y;

System.out.println("Answer = " + answer);

For Subtraction

Only answer = x - y; only needs to be changed. Answer -10

Only answer = x * y; only needs to be changed. Answer 600


But in the case of dividing, since int is selected only intiger number
will be displayed as answer. If there is decimal, it will show the
answer as ‘0’

Ex: 20/30 = in java it will be ‘0’

But if it is like 60/30 = 2

If 70/30, the answer will be 2 because since this is int decimal


cannot be displayed. So in this case change ‘int’ with ‘double’.

double x, y, answer;
x = 20;
y = 30;
answer = x / y;

System.out.println("Answer = " + answer);

Another option is modulas operator in Java, which is simply the


remender.
https://www.youtube.com/watch?v=f5YdkIzNmfM&list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-
Al&index=7

Increment Operator and Assignment


Operator

Method One

public static void main(String[] args) {

int x = 10;
x = x + 1;
System.out.println(x);

Answer is 11

Method One
public static void main(String[] args) {

int x = 10;
x++; (This is called post increment operation)
System.out.println(x);

Answer is 11
Another Option

public static void main(String[] args) {

int x = 10;

System.out.println(x++);
System.out.println(x); (this is called pro post implementation)

}
So this is ass follows:

X++ will show 10

X will show 11 below 10

On the other hand

++x will directly show 11 (this is called pre increment operator)

If I want to add 5 there are multiple ways:

int x = 10;

x = x + 5 (one method)

x +=5; (This is x = x +5 itself)

x *=5; (This is x = x *5 itself)

x /=5; (This is x = x /5 itself)

x -=5; (This is x = x - 5 itself)

In Java ‘=’ is called ‘assignment’ operator


https://www.youtube.com/watch?v=f5YdkIzNmfM&list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-
Al&index=8

IF ... ELSE Statements and Relational


Operators
‘==’ this is comparison operator
package lesson1;

public class MyClass {


/*
If (boolean_expression)
{
execute of the boolean expression is true
}
*/
public static void main(String[] args) {
int x = 10;
if (x == 10) {
System.out.println("yes x == 10");
}

‘!=’ this is called non equality operator

public static void main(String[] args) {


int x = 10;
if (x == 20) {
System.out.println("yes x == 10");
}
else {
System.out.println("no x != 10");
}

Notes:
Here in the first example, the program is executed because the value
of ‘x’ is 10. But in the second operation only the second program is
executed because the value of ‘x’ is not 10.

Different types of comparison operator:

== is equal to
!= is not equal to
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to

You might also like