




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive introduction to fundamental java programming concepts, covering data types, variables, and control flow. It explains the distinction between primitive and reference data types, explores variable declaration and initialization, and delves into various control flow structures like if-else statements, loops, and jump statements. Suitable for beginners learning java programming and provides a solid foundation for further exploration of the language.
Typology: Slides
1 / 36
This page cannot be seen from the preview
Don't miss anything!
JDK(Java Development Kit): JDK is intended for software developers and includes development tools such as the Java compiler, Javadoc, Jar, and a debugger. JRE(Java Runtime Environment): JRE contains the parts of the Java libraries required to run Java programs and is intended for end-users. JRE can be viewed as a subset of JDK. JVM: JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which java bytecode can be executed. JVMs are available for many hardware and software platforms. How is Java executed
Data Types Java is statically typed : type of every variable is known at compile-time. This means that you must explicitly declare the type of a variable when you write your code. compiler checks types when you compile the program, ensuring that type errors are caught before the program is run. strongly typed language : strict rules about how types are used, and it doesn’t allow implicit or automatic type conversions that could result in unexpected behavior. Data Types in Java
Data Types Two major data types: Primitive: o nly single values and have no special capabilities. There are 8 primitive data types. Non primitive or Reference Data Types: will contain a memory address of variable values because the reference types won’t store the variable value directly in memory. They are strings, objects, arrays, etc. Data Types in Java
Type Description Default Size Example Literals Range of values boolean true or false false 8 bits true, false true, false byte twos-complement integer 0 8 bits (none) -128 to 127 char Unicode character \u0000 16 bits ‘a’, ‘\u0041’, ‘\101’, ‘\’, ‘\’, ‘\n’, ‘β’ characters representation of ASCII values 0 to 255 short twos-complement integer 0 16 bits (none) -32,768 to 32,
Type Description Default Size Example Literals Range of values int twos-complement intger 0 32 bits -2,-1,0,1, -2,147,483, to 2,147,483, long twos-complement integer 0 64 bits -2L,-1L,0L,1L,2L -9,223,372,036,854, , to 9,223,372,036,854,775, 807 float IEEE 754 floating point 0.0 32 bits 1.23e100f , -1.23e-100f , .3f ,3.14F upto 7 decimal digits double IEEE 754 floating point 0.0 64 bits 1.23456e300d , -123456e-300d , 1e1d upto 16 decimal digits
Initialisation of variable Local Variables : defined within a block or method or constructor is called a local variable
Instance Variables: non-static variables and are declared in a class outside of any method, constructor, or block.
Static Variables: class variables. These variables are declared similarly to instance variables. The difference is that static variables are declared using the static keyword within a class outside of any method, constructor, or block.
Types of Variables in Java int num = 10; dataType varName = value;
Each object will have its own copy of an instance variable, whereas we can only have one copy of a static variable per class, irrespective of how many objects we create. Thus, static variables are good for memory management.
Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of the instance variable. In the case of a static variable, changes will be reflected in other objects as static variables are common to all objects of a class.
We can access instance variables through object references, and static variables can be accessed directly using the class name.
Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed. Static variables are created when the program starts and destroyed when the program stops.
Differences Between the Instance Variables and the Static Variables
Postincrement : 10 Preincrement : 11 Postdecrement : 10 Predecrement : 9 a = 10, a++ (++a) a-- --a
- : Unary minus -negating the values. + : Unary plus -positive value , automatic conversion to int when the type of its operand is the byte, char, or short. (unary numeric promotion). ++ : Increment operator, incrementing value by 1. There are two varieties of increment operators. Post-Increment: Value first used for computing the result and then incremented. Pre-Increment: Value incremented first, and then result is computed. - – : Decrement operator, used for decrementing the value by 1. There are two varieties of decrement operators. Post-decrement: Value first used for computing result and then decremented. Pre-Decrement: The value decremented first, and then result is computed. ! : Logical not operator,inverting a boolean value. Unary Operators in Java boolean isStudent = true !isStudent (=> false)
relations like equality, greater than, and less than. They return boolean results after the comparison and are extensively used in looping statements as well as conditional if-else statements. ==, Equal to returns true if the left-hand side is equal to the right-hand side. !=, Not Equal to returns true if the left-hand side is not equal to the right-hand side. <, less than: returns true if the left-hand side is less than the right-hand side. <=, less than or equal to returns true if the left-hand side is less than or equal to the right-hand side. >, Greater than: returns true if the left-hand side is greater than the right-hand side. >=, Greater than or equal to returns true if the left-hand side is greater than or equal to the right-hand side. variable relation_operator value Relational Operators in Java a > b: true a < b: false a >= b: true a <= b: false a == c: false a != c: true a = 10, b = 3, c = 5
perform “logical AND” and “logical OR” operations, second condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Java also has “Logical NOT”, which returns true when the condition is false and vice-versa &&, Logical AND: returns true when both conditions are true. ||, Logical OR: returns true if at least one condition is true. !, Logical NOT: returns true when a condition is false and vice- versa variable relation_operator value Logical Operators in Java x && y: false x || y: true !x: false x = true, y = false
perform the manipulation of individual bits of a number. &, Bitwise AND operator: returns bit by bit AND of input values. |, Bitwise OR operator: returns bit by bit OR of input values. ^, Bitwise XOR operator : returns bit-by-bit XOR of input values. ~, Bitwise Complement Operator: This is a unary operator which returns the one’s complement representation of the input value, i.e., with all bits inverted. Bitwise Operators in Java and = 00100 or = 11110 xor = 11010 a (not) = 01001 b = 01100 a = 10110
shift the bits of a number left or right, thereby multiplying or dividing the number by two, respectively. <<, Left shift operator: shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as multiplying the number with some power of two. >>, Signed Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of the initial number. Similar effect to dividing the number with some power of two. >>>, Unsigned Right shift operator: shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit is set to 0. Shift Operators in Java 10 >> 1 = 5 101 10 << 1 = 20 10100 number shift_op number_of_places_to_shift; 8 = bin(1000) 1 * 2^3 + 02^2 + 02^1 + 02^ 10 = bin(1010) 1234 = 110^3 + 210^2 + 310^1 + 4*10^