SlideShare una empresa de Scribd logo
1 de 23
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
java token
java tokenjava token
java token
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
JVM
JVMJVM
JVM
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Java data types, variables and jvm
Java data types, variables and jvm Java data types, variables and jvm
Java data types, variables and jvm
 
Data types in java
Data types in javaData types in java
Data types in java
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Object Oriented Programming Languages
Object Oriented Programming LanguagesObject Oriented Programming Languages
Object Oriented Programming Languages
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Core java
Core javaCore java
Core java
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Java exception
Java exception Java exception
Java exception
 

Similar a Fundamental programming structures in java

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...GANESHBABUVelu
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and typesDaman Toor
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio) rnkhan
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)rnkhan
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentationAdhoura Academy
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptmanish kumar
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data TypesTareq Hasan
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2dplunkett
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1Todor Kolev
 

Similar a Fundamental programming structures in java (20)

Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) I...
 
Basic_Java_02.pptx
Basic_Java_02.pptxBasic_Java_02.pptx
Basic_Java_02.pptx
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
 
Java Programming
Java Programming Java Programming
Java Programming
 
Java script final presentation
Java script final presentationJava script final presentation
Java script final presentation
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
Java: Primitive Data Types
Java: Primitive Data TypesJava: Primitive Data Types
Java: Primitive Data Types
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Md03 - part3
Md03 - part3Md03 - part3
Md03 - part3
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
 
JAVASCRIPT - LinkedIn
JAVASCRIPT - LinkedInJAVASCRIPT - LinkedIn
JAVASCRIPT - LinkedIn
 
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
 

Más de Shashwat Shriparv (20)

Learning Linux Series Administrator Commands.pptx
Learning Linux Series Administrator Commands.pptxLearning Linux Series Administrator Commands.pptx
Learning Linux Series Administrator Commands.pptx
 
LibreOffice 7.3.pptx
LibreOffice 7.3.pptxLibreOffice 7.3.pptx
LibreOffice 7.3.pptx
 
Kerberos Architecture.pptx
Kerberos Architecture.pptxKerberos Architecture.pptx
Kerberos Architecture.pptx
 
Suspending a Process in Linux.pptx
Suspending a Process in Linux.pptxSuspending a Process in Linux.pptx
Suspending a Process in Linux.pptx
 
Kerberos Architecture.pptx
Kerberos Architecture.pptxKerberos Architecture.pptx
Kerberos Architecture.pptx
 
Command Seperators.pptx
Command Seperators.pptxCommand Seperators.pptx
Command Seperators.pptx
 
Upgrading hadoop
Upgrading hadoopUpgrading hadoop
Upgrading hadoop
 
Hadoop migration and upgradation
Hadoop migration and upgradationHadoop migration and upgradation
Hadoop migration and upgradation
 
R language introduction
R language introductionR language introduction
R language introduction
 
Hive query optimization infinity
Hive query optimization infinityHive query optimization infinity
Hive query optimization infinity
 
H base introduction & development
H base introduction & developmentH base introduction & development
H base introduction & development
 
Hbase interact with shell
Hbase interact with shellHbase interact with shell
Hbase interact with shell
 
H base development
H base developmentH base development
H base development
 
Hbase
HbaseHbase
Hbase
 
H base
H baseH base
H base
 
My sql
My sqlMy sql
My sql
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
 
Introduction to apache hadoop
Introduction to apache hadoopIntroduction to apache hadoop
Introduction to apache hadoop
 
Next generation technology
Next generation technologyNext generation technology
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} };