Java
Tutotrial for [NLP-AI]
2
ashishfa@[Link]
December 18, 2020 1
asgn1
asgn2
Last Assignment
? Print the details in tabular format
- use tabs \t
[Link]("\"Can
we print '\\' with
[Link]() statement?\"" );
December 18, 2020 Java Tutorial series part 2 2
Storing data
In Java, Data can be stored as
Numbers
2, 6.67, 0.009
Characters
‘c’, ‘p’, ‘?’, ‘2’
Strings
“data”, “Hindi language”, “$99”, “[Link]”
December 18, 2020 Java Tutorial series part 2 3
int1
Program int2
// this program will declare and print a number
class int2
{
public static void main(String[] arguments)
{
int weight = 68;
[Link]("your weight is " + weight);
}
}
//end of program
December 18, 2020 Java Tutorial series part 2 4
Comments(double slash)
Not executed
For documentation
Can be placed on any line
Anything written after // is not executed
E.g.
// this is a comment
Used to explain/describe what is done in the
program
Good practice
December 18, 2020 Java Tutorial series part 2 5
float1
Types of numbers
int
Whole numbers like 0, 575, -345 etc.
double
Numbers with decimal point
like 12.453, 3.432, 0.0000002
December 18, 2020 Java Tutorial series part 2 6
Variable declaration
Before using any name, it must be declared
(with its type i.e int or double).
Needed only once in one program
Generally, done initially
Syntax
datatype name;
double total; // stores the total value
int index;
int a,b , c, sum, interest;
December 18, 2020 Java Tutorial series part 2 7
int3
Assignment int5
int a; //declaration – needed once
a = 10 ; // assignment … declared above
int a = 10; // assignment and declaration together
10 = a ; // not possible – compilation error
Left hand side is always a variable for assignment
Storage area
a 10
December 18, 2020 Java Tutorial series part 2 8
int4
Assignment…
int a , b ; // a =? b = ?
a = 4; // a = 4 b = ?
b = 7; // a = 4 b = 7
a = b; // a = 7 b = 7
b = a; // a = 7 b = 7
a = 5; // a = 5 b = 7
December 18, 2020 Java Tutorial series part 2 9
char1
Character data char2
Characters
‘a’, ‘A’, ‘c’ , ‘?’ , ‘3’ , ‘ ’
(last is the single space)
Enclosed in single quotes
Character variable declaration
char ch;
Character assignment
ch = ‘k’;
December 18, 2020 Java Tutorial series part 2 10
string1
string2
String data string3
Strings are sequence of characters enclosed in double quotes
Declaration
String name;
String address;
String line;
Assignment
name = “ram”;
line = “this is a line with spaces”;
name = “a”; // single character can be stored
name = “”; // empty string
The sequence of characters enclosed in double quotes, printed in println()
are also strings.
E.g. [Link]( “Welcome ! “ );
December 18, 2020 Java Tutorial series part 2 11
Practice problem
Try to print these two figures on the screen using println and least number
of strings
*******
*****
***
*
***
*****
*******
**********
* *
* *
* *
**********
December 18, 2020 Java Tutorial series part 2 12
Thank you
December 18, 2020 Java Tutorial series part 2 13