SlideShare a Scribd company logo
Shashwat Shriparv
dwivedishashwat@gmail.com
InfinitySoft
Contents
• A Simple Java Program
• Comments
• Data Types
• Variables
• Assignments and Initializations
• Operators
• Strings
• Control Flow
• Big Numbers
• Arrays
A Simple Java Program
public class FirstSample
{
public static void main(String[] args)
{
System.out.println("We will not use 'Hello, World!'");
}
}
•Java is case sensitive.
•The keyword public is called an access
modifier.
•Everything in a Java program must be
inside a class.
Comments
Comments in Java, like comments in most
programming languages, do not show up in the
executable program.
Java has three ways of showing comments.
1. The most common method is a //.
2. When longer comments are needed, you can use
the /* and */ comment delimiters that let you block
off a longer comment.
3. Finally, there is a third kind of comment that
can be used to generate documentation
automatically. This comment uses a /** to start and
a */ to end.
Data Types
Java is a strongly typed language.
There are eight primitive types in Java.
• Four of them are integer types;
• Two are floating-point number types;
• One is the character type char,
• One is a boolean type for truth values.
Integers
The integer types are for numbers without
fractional parts.
Type Storage
Requirement
Range (inclusive)
int 4 bytes
–2,147,483,648 to 2,147,483,
647 (just over 2 billion)
short 2 bytes –32,768 to 32,767
long 8 bytes
–9,223,372,036,854,775,808L
to 9,223,372,036,854,775,807L
byte 1 byte –128 to 127
Floating-Point Types
The floating-point types denote numbers with
fractional parts.
Type
Storage
Requirement Range
float 4 bytes
approximately ±3.40282347E+38F (6–7
significant decimal digits)
double 8 bytes
approximately
±1.79769313486231570E+308 (15
significant decimal digits)
The Character Type
Single quotes are used to denote char constants.
The char type denotes characters in the
Unicode encoding scheme.
Special characters
Escape Sequence Name Unicode Value
b backspace u0008
t tab u0009
n linefeed u000a
r carriage return u000d
" double quote u0022
' single quote u0027
 backslash u005c
The Boolean Type
The boolean type has two values, false and true.
It is used for evaluating logical conditions.
Variables
In Java, every variable has a type.
Eg: double salary;
int vacationDays;
long earthPopulation;
char yesChar;
boolean done;
The rules for a variable name are as follows:
A variable name must begin with a letter, and must
be a sequence of letters or digits.
Symbols like '+' or '©' cannot be used inside variable
names, nor can spaces.
All characters in the name of a variable are significant
and case is also significant.
Cannot use a Java reserved word for a variable name.
Can have multiple declarations on a single line
Assignments and Initializations
After you declare a variable, you must
explicitly initialize it by means of an
assignment statement.
You assign to a previously declared
variable using the variable name on the left,
an equal sign (=) and then some Java expression
that has an appropriate value on the right.
int vacationDays; // this is a declaration
vacationDays = 12; // this is an assignment
One nice feature of Java is the ability to both
declare and initialize a variable on the same line.
For example:
int vacationDays = 12; // this is an initialization
Operators
Operator Function
+ Addition
- Subtraction
* Multiplication
/ Division
% Integer remainder
There is a convenient shortcut for using
binary arithmetic operators in an assignment.
For example,
x += 4; is equivalent to x = x + 4;
Increment and Decrement Operators
x++ adds 1 to the current value of the variable x,
and x-- subtracts 1 from it. They cannot be applied to
numbers themselves.
int m = 7, n = 7;
int a = 2 * ++m;
int b = 2 * n++;
There are actually two forms of these operators;
you have seen the “postfix” form of the operator
that is placed after the operand. There is also a
“Prefix” form, ++n. Both change the value of the
variable by 1.
Relational and boolean Operators
Java has the full complement of relational operators.
To test for equality, a double equal sign, “ == ” is used.
A “ != ” is used for checking inequality.
< (less than), > (greater than), <= (less than or equal),
and >= (greater than or equal) operators.
&& for the logical “and” operator
|| for the logical “or” operator
! is the logical negation operator.
Ternary operator, ?:
condition ? e1 : e2
Evaluates to e1 if the condition is true, to e2 otherwise.
Bitwise Operators
& ("and")
| ("or")
^ ("xor")
~ ("not")
>> right shift
<< left shift
Operator Purpose
+ addition of numbers, concatenation of Strings
+= add and assign numbers, concatenate and
assign Strings
- subtraction
-= subtract and assign
* multiplication
*= multiply and assign
/ division
/= divide and assign
% take remainder
%= take remainder and assign
++ increment by one
-- decrement by one
Operator Purpose
> greater than
>= greater than or equal to
< less than
<= less than or equal to
! boolean NOT
!= not equal to
&& boolean AND
|| boolean OR
== boolean equals
= assignment
~ bitwise NOT
?: conditional
Operator Purpose
| bitwise OR
|= bitwise OR and assign
^ bitwise XOR
^= bitwise XOR and assign
& bitwise AND
&= bitwise AND and assign
>> shift bits right with sign extension
>>= shift bits right with sign extension
and assign
<< shift bits left
<<= shift bits left and assign
>>> unsigned bit shift right
>>>= unsigned bit shift right and assign
Strings
Strings are sequences of characters, such as "Hello".
Java does not have a built-in string type. Instead, the
standard Java library contains a predefined class called,
String.
String e = ""; // an empty string
String greeting = "Hello";
Concatenation
String expletive = "Expletive";
String PG13 = "deleted";
String message = expletive + PG13;
Substrings
String greeting = "Hello";
String s = greeting.substring(0, 4);
Control Flow
Java, like any programming language, supports both
conditional statements and loops to determine control flow.
The Java control flow constructs are identical to those in
C and C++, with some exceptions. There is no goto, but
there is a “labeled” version of break that you can use to
break out of a nested loop
Conditional Statements
if
if- else
while
do… while
for
switch
Big Numbers
If the precision of the basic integer and
floating-point types is not sufficient, you can turn to a
couple of handy classes in the java.math package, called
BigInteger and BigDecimal.
These are classes for manipulating numbers with an
arbitrarily long sequence of digits.
Use the static valueOf method to turn an ordinary
number into a big number:
BigInteger a = BigInteger.valueOf(100);
You cannot use the familiar mathematical operators such
as + and * to combine big numbers. Instead, you must use
methods such as add and multiply in the big number classes.
BigInteger c = a.add(b); // c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));
// d = c * (b + 2)
Arrays
An array is a data structure that stores a collection of
values of the same type.
You access each individual value through an
integer index.
Declaration of an array a of integers:
int[] a; or int a[];
However, this statement only declares the variable a.
It does not yet initialize a with an actual array. You use
the new operator to create the array.
int[] a = new int[100];
Java has a shorthand to create an array object and supply
initial values at the same time.
int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
Sorting an Array
If you want to sort an array of numbers, you can
use one of the sort methods in the Arrays class:
int[] a = new int[10000];
. . .
Arrays.sort(a)
This method uses a tuned version of the QuickSort
algorithm that is claimed to be very efficient on
most data sets.
Multidimensional Arrays
Multidimensional arrays use more than one index
to access array elements. They are used for tables and other
more complex arrangements.
Declaring a matrix in Java is simple enough.
For example:
double[][] balance;
The initialization as follows:
balance = new double[NYEARS][NRATES];
A shorthand notion for initializing multidimensional arrays
without needing a call to new.
For example;
int[][] magicSquare =
{
{16, 3, 2, 13},
{5, 10, 11, 8},
{9, 6, 7, 12},
{4, 15, 14, 1}
};
Shashwat Shriparv
dwivedishashwat@gmail.com
InfinitySoft

More Related Content

PPTX
Inheritance in java
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Core java complete ppt(note)
DOC
Time and space complexity
PPTX
Arrays in Java
PPTX
Strings in Java
PPTX
Abstract class in c++
PPTX
This keyword in java
Inheritance in java
Basic Concepts of OOPs (Object Oriented Programming in Java)
Core java complete ppt(note)
Time and space complexity
Arrays in Java
Strings in Java
Abstract class in c++
This keyword in java

What's hot (20)

PPT
Function overloading(c++)
PPTX
Static keyword ppt
PPTX
Java string handling
PDF
Remote Method Invocation in JAVA
PPTX
Inheritance in JAVA PPT
PPTX
Basics of JAVA programming
PPTX
Super keyword in java
PPTX
Java package
PPS
Interface
PPTX
Presentation on java project (bank management system)
PPTX
What is component in reactjs
PPTX
java mini project for college students
PPTX
Need of object oriented programming
PPTX
Data members and member functions
PPTX
Inheritance in c++
PPT
Student management system project report c++
PPTX
Interface in java
PPTX
Java project-presentation
PPTX
array of object pointer in c++
Function overloading(c++)
Static keyword ppt
Java string handling
Remote Method Invocation in JAVA
Inheritance in JAVA PPT
Basics of JAVA programming
Super keyword in java
Java package
Interface
Presentation on java project (bank management system)
What is component in reactjs
java mini project for college students
Need of object oriented programming
Data members and member functions
Inheritance in c++
Student management system project report c++
Interface in java
Java project-presentation
array of object pointer in c++
Ad

Similar to Fundamental programming structures in java (20)

PPTX
Java chapter 2
PPTX
Java Module 2 -Vikas.pptx About Java Programming
PDF
data types.pdf
PDF
03-Primitive-Datatypes.pdf
PDF
Chapter 01 Introduction to Java by Tushar B Kute
PPTX
03 and 04 .Operators, Expressions, working with the console and conditional s...
PPTX
ChapterTwoandThreefnfgncvdjhgjshgjdlahgjlhglj.pptx
PPTX
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
PPTX
Java Basic Elements Lecture on Computer Science
PDF
Java q ref 2018
PPSX
DISE - Windows Based Application Development in Java
PPT
java tutorial 2
PPTX
java-tokens-data-types.pptx ciiiidddidifif
PPTX
Java Basics 1.pptx
PPT
PRAGRAMMING IN JAVA (BEGINNER)
PPTX
MODULE_2_Operators.pptx
PPTX
Data Types, Variables, and Operators
PPTX
JPC#8 Introduction to Java Programming
Java chapter 2
Java Module 2 -Vikas.pptx About Java Programming
data types.pdf
03-Primitive-Datatypes.pdf
Chapter 01 Introduction to Java by Tushar B Kute
03 and 04 .Operators, Expressions, working with the console and conditional s...
ChapterTwoandThreefnfgncvdjhgjshgjdlahgjlhglj.pptx
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java Basic Elements Lecture on Computer Science
Java q ref 2018
DISE - Windows Based Application Development in Java
java tutorial 2
java-tokens-data-types.pptx ciiiidddidifif
Java Basics 1.pptx
PRAGRAMMING IN JAVA (BEGINNER)
MODULE_2_Operators.pptx
Data Types, Variables, and Operators
JPC#8 Introduction to Java Programming
Ad

More from Shashwat Shriparv (20)

PPTX
Learning Linux Series Administrator Commands.pptx
PPTX
LibreOffice 7.3.pptx
PPTX
Kerberos Architecture.pptx
PPTX
Suspending a Process in Linux.pptx
PPTX
Kerberos Architecture.pptx
PPTX
Command Seperators.pptx
DOCX
Upgrading hadoop
PPTX
Hadoop migration and upgradation
PPTX
R language introduction
PPTX
Hive query optimization infinity
PPTX
H base introduction & development
PPTX
Hbase interact with shell
PPT
H base development
PPTX
PPTX
PPTX
Apache tomcat
PPTX
Linux 4 you
PDF
Introduction to apache hadoop
DOCX
Next generation technology
Learning Linux Series Administrator Commands.pptx
LibreOffice 7.3.pptx
Kerberos Architecture.pptx
Suspending a Process in Linux.pptx
Kerberos Architecture.pptx
Command Seperators.pptx
Upgrading hadoop
Hadoop migration and upgradation
R language introduction
Hive query optimization infinity
H base introduction & development
Hbase interact with shell
H base development
Apache tomcat
Linux 4 you
Introduction to apache hadoop
Next generation technology

Fundamental programming structures in java

  • 2. Contents • A Simple Java Program • Comments • Data Types • Variables • Assignments and Initializations • Operators • Strings • Control Flow • Big Numbers • Arrays
  • 3. A Simple Java Program public class FirstSample { public static void main(String[] args) { System.out.println("We will not use 'Hello, World!'"); } } •Java is case sensitive. •The keyword public is called an access modifier. •Everything in a Java program must be inside a class.
  • 4. Comments Comments in Java, like comments in most programming languages, do not show up in the executable program. Java has three ways of showing comments. 1. The most common method is a //. 2. When longer comments are needed, you can use the /* and */ comment delimiters that let you block off a longer comment. 3. Finally, there is a third kind of comment that can be used to generate documentation automatically. This comment uses a /** to start and a */ to end.
  • 5. Data Types Java is a strongly typed language. There are eight primitive types in Java. • Four of them are integer types; • Two are floating-point number types; • One is the character type char, • One is a boolean type for truth values.
  • 6. Integers The integer types are for numbers without fractional parts. Type Storage Requirement Range (inclusive) int 4 bytes –2,147,483,648 to 2,147,483, 647 (just over 2 billion) short 2 bytes –32,768 to 32,767 long 8 bytes –9,223,372,036,854,775,808L to 9,223,372,036,854,775,807L byte 1 byte –128 to 127
  • 7. Floating-Point Types The floating-point types denote numbers with fractional parts. Type Storage Requirement Range float 4 bytes approximately ±3.40282347E+38F (6–7 significant decimal digits) double 8 bytes approximately ±1.79769313486231570E+308 (15 significant decimal digits)
  • 8. The Character Type Single quotes are used to denote char constants. The char type denotes characters in the Unicode encoding scheme. Special characters Escape Sequence Name Unicode Value b backspace u0008 t tab u0009 n linefeed u000a r carriage return u000d " double quote u0022 ' single quote u0027 backslash u005c The Boolean Type The boolean type has two values, false and true. It is used for evaluating logical conditions.
  • 9. Variables In Java, every variable has a type. Eg: double salary; int vacationDays; long earthPopulation; char yesChar; boolean done; The rules for a variable name are as follows: A variable name must begin with a letter, and must be a sequence of letters or digits. Symbols like '+' or '©' cannot be used inside variable names, nor can spaces. All characters in the name of a variable are significant and case is also significant. Cannot use a Java reserved word for a variable name. Can have multiple declarations on a single line
  • 10. Assignments and Initializations After you declare a variable, you must explicitly initialize it by means of an assignment statement. You assign to a previously declared variable using the variable name on the left, an equal sign (=) and then some Java expression that has an appropriate value on the right. int vacationDays; // this is a declaration vacationDays = 12; // this is an assignment One nice feature of Java is the ability to both declare and initialize a variable on the same line. For example: int vacationDays = 12; // this is an initialization
  • 11. Operators Operator Function + Addition - Subtraction * Multiplication / Division % Integer remainder There is a convenient shortcut for using binary arithmetic operators in an assignment. For example, x += 4; is equivalent to x = x + 4; Increment and Decrement Operators x++ adds 1 to the current value of the variable x, and x-- subtracts 1 from it. They cannot be applied to numbers themselves.
  • 12. int m = 7, n = 7; int a = 2 * ++m; int b = 2 * n++; There are actually two forms of these operators; you have seen the “postfix” form of the operator that is placed after the operand. There is also a “Prefix” form, ++n. Both change the value of the variable by 1. Relational and boolean Operators Java has the full complement of relational operators. To test for equality, a double equal sign, “ == ” is used. A “ != ” is used for checking inequality. < (less than), > (greater than), <= (less than or equal), and >= (greater than or equal) operators.
  • 13. && for the logical “and” operator || for the logical “or” operator ! is the logical negation operator. Ternary operator, ?: condition ? e1 : e2 Evaluates to e1 if the condition is true, to e2 otherwise. Bitwise Operators & ("and") | ("or") ^ ("xor") ~ ("not") >> right shift << left shift
  • 14. Operator Purpose + addition of numbers, concatenation of Strings += add and assign numbers, concatenate and assign Strings - subtraction -= subtract and assign * multiplication *= multiply and assign / division /= divide and assign % take remainder %= take remainder and assign ++ increment by one -- decrement by one
  • 15. Operator Purpose > greater than >= greater than or equal to < less than <= less than or equal to ! boolean NOT != not equal to && boolean AND || boolean OR == boolean equals = assignment ~ bitwise NOT ?: conditional
  • 16. Operator Purpose | bitwise OR |= bitwise OR and assign ^ bitwise XOR ^= bitwise XOR and assign & bitwise AND &= bitwise AND and assign >> shift bits right with sign extension >>= shift bits right with sign extension and assign << shift bits left <<= shift bits left and assign >>> unsigned bit shift right >>>= unsigned bit shift right and assign
  • 17. Strings Strings are sequences of characters, such as "Hello". Java does not have a built-in string type. Instead, the standard Java library contains a predefined class called, String. String e = ""; // an empty string String greeting = "Hello"; Concatenation String expletive = "Expletive"; String PG13 = "deleted"; String message = expletive + PG13; Substrings String greeting = "Hello"; String s = greeting.substring(0, 4);
  • 18. Control Flow Java, like any programming language, supports both conditional statements and loops to determine control flow. The Java control flow constructs are identical to those in C and C++, with some exceptions. There is no goto, but there is a “labeled” version of break that you can use to break out of a nested loop Conditional Statements if if- else while do… while for switch
  • 19. Big Numbers If the precision of the basic integer and floating-point types is not sufficient, you can turn to a couple of handy classes in the java.math package, called BigInteger and BigDecimal. These are classes for manipulating numbers with an arbitrarily long sequence of digits. Use the static valueOf method to turn an ordinary number into a big number: BigInteger a = BigInteger.valueOf(100); You cannot use the familiar mathematical operators such as + and * to combine big numbers. Instead, you must use methods such as add and multiply in the big number classes. BigInteger c = a.add(b); // c = a + b BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)
  • 20. Arrays An array is a data structure that stores a collection of values of the same type. You access each individual value through an integer index. Declaration of an array a of integers: int[] a; or int a[]; However, this statement only declares the variable a. It does not yet initialize a with an actual array. You use the new operator to create the array. int[] a = new int[100]; Java has a shorthand to create an array object and supply initial values at the same time. int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
  • 21. Sorting an Array If you want to sort an array of numbers, you can use one of the sort methods in the Arrays class: int[] a = new int[10000]; . . . Arrays.sort(a) This method uses a tuned version of the QuickSort algorithm that is claimed to be very efficient on most data sets. Multidimensional Arrays Multidimensional arrays use more than one index to access array elements. They are used for tables and other more complex arrangements.
  • 22. Declaring a matrix in Java is simple enough. For example: double[][] balance; The initialization as follows: balance = new double[NYEARS][NRATES]; A shorthand notion for initializing multidimensional arrays without needing a call to new. For example; int[][] magicSquare = { {16, 3, 2, 13}, {5, 10, 11, 8}, {9, 6, 7, 12}, {4, 15, 14, 1} };