0% found this document useful (0 votes)
15 views10 pages

Java

The document provides an overview of Java programming structure, including class declaration, the main method, and keywords. It explains the importance of data types, type casting, and variables, along with examples of primitive and non-primitive data types. Additionally, it outlines steps to compile and run a Java program and introduces concepts like constants and bitwise operators.

Uploaded by

nafia822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Java

The document provides an overview of Java programming structure, including class declaration, the main method, and keywords. It explains the importance of data types, type casting, and variables, along with examples of primitive and non-primitive data types. Additionally, it outlines steps to compile and run a Java program and introduces concepts like constants and bitwise operators.

Uploaded by

nafia822
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
we 1.3 Java program Structure “class stDemo { a public static void main(String args[]) { [Link] (“Hello JAVA’); Corefoa (1 ng oor owas Nm, , Step 5: Ns } step 6:¥ et % aes (Wer 36-0-35003} . oa ryt Corporat is ALT THEN reser yey 3. a Ast users : a patb-c:Wrogra FL1eS\36V04.8.0 s4\bin examp! 28 ae ens systemout printing. In 1. lass keyword is used to declare a class in java. line ce ; and at 2 public keyword is an access modifier which represents visibility, it means itis visible to al 3. staticis a keyword, if we declare any method as static, itis known as static method ‘The core advantage of static method is that there is no need to create objectt» | invoke the static method. The main method is executed by the JYM, so it doesst_ | equire to create object to invoke the main method. So it saves memory. | * void isthe return type of the method, it means it doesn't return any value. main represents startup of the program. String arg is used for command line argument. We will learn i ater. ‘[Link]() is used print statement. | | 1.4 Steps to Compile and Run your First Java Program. le StP 1 Open a text editor and write the code as above ( Step 2 : Save the file as stDemo java = Step 3: Open command Prompt and go to the directory where you saved your first jan) Program assuming it saved in D\ 2 Step 4 : Type javac . stDemojava de, This 2 dl cate ‘ava and press Enter KEY to compile your co: WO apie ei eae Ea a is | Multiline comment | ue __An identifier is a name given by the programmer to elements like variabl methods, classes, and objects. ‘ les, iT first), ae ee Start with a letter or underscore. CO; de. Use letters, digits, or underscores, f there» Case-sensitive, no spaces/special chars. Use meaningful names. 6 rrr Core Java { Legal: age, $sname, _stud, mat_value Illegal : 123abc, -stud 1.8 Keywords Keywords are reserved words in Java that have a predefined me be used as identifiers, Example: int result: de ords hb [1.8 Kevw voserved words in Java that have a predefined meaning, They can, ed Keywordsare reserv ° bo be used as identifiers. Example: 1.9.2 Ne int result; Non-} Here intis a keyword. programn reference bool: break short strictfp | long Example abstract | assert oolean | byte case catch char switch this protectej e Si ; oa dass [const —_| continue | default throws try while ec | eo double | else enum volatile super | package - t extends | final finally | float static throw | return | 6 | implements | synchronized | void | new import | instanceof | int i : interface transient private | public Ava and its v: z Example Java isa strong] ‘ i 'Y typed, object-orie, Java is ¢; vari inted pro, ; : ae be declared with a specific ae eae language. This means eve! 5 fe the 7 ‘a type F Pe of data a variable can ho} Pe before it can be used, Data typ’ amountof memory, id, the emory it occupies, Tange ofvahies it Gants s/ .9 Data yr Java is a strongly typed, object-oriented programming language. This means ey, variable must be declared with a specific data type before it can be used, Data typ define the type of data a variable can hold, the range of values it can store, ae Y lt amount of memory it occupies. 1.9.1 Primitive Data Types Java provides eight primitive data types which are built into the language. These are the most basic forms of data representation. byte ~ 8-bit signed integer, range : -128 to 127. e short > 16-bit signed integer, range : -32,768 to 32,767. © int > 32-bit signed integer, commonly used for whole numbers. onimiereeing =m, 4.11.7 Ternary Operator A shorthand for else that evaluates condition ana * pentctnt mand returns on. yen variable = (condition) ? value [Link] value 4 FW vale, # HARD Lae; Sg Woda Example: i intma neaas ou by Nar Aso __/ )rypes ofliterals Se Che tntegeriteral: 1 a 46 Floating point teal: 3.14, -05 . Ma F 6 characterliteral:"A% 0, rxamp : to tring tral tn Boolean literal: true, false Nuatuiterat: null Samp 1.12.2 Constants : A constant isa variable whose value cannot be changed once assigned. 1? ‘© Declared using the fina keyword. dass Must be initialized when declared « P Naming convention: all uppercase letters with underscores Example: ‘ final double P= 3.14159; final int MAX_USER' ‘Type Casting is the process of converting a value of one data type into another. ‘Types of Type Casting: ) Widening Casting (Implicit) © Also called automatic type conversion. Converts a smaller data type into a larger data type. Bites Siesta ae a ee: on orion ove Proper wwe © Happens automatically by Java © Nodata loss, Order: byte-+ short ->int~+ long.-+ float -+ double Example: inta= 10; double b= a; // ints converted to double automatically 1b) Narrowing Casting (Explicit) ‘© Also called manual type conversion, © Converts a larger datatype into a smaller data type. ‘¢ Must be done manually using parentheses. © May cause data loss Example: double a=978; int b= (nt) a; // double is converted to int explicitly ‘Sample Codes Or // Program to demonstrate bitwise operators in Java class BitwiseOperatorsExample { public static vold main(String] args) c inta=5; // Binary: 0101 intb=3; // Binary: 0011 Systemoutprintin(‘a & b=" + (ab); // AND systemoutprintin(a|b="+ (a1): //OR systemoutprintin('a* b=" +(ab));// XOR + (a) // NOT “+ (a << 1); // Left shit =" (a> 1));// Right shift systemout printin( [Link]("a << systemoutprintin('a>> 7 ore Jor 82.02 \< a In this code- & — Bitwise AND: sets each bit to 1 if both bits are 1. | > Bitwise OR: sets each bit to 1 if any bit is 1. = Bitwise XOR: sets each bit to 1 if bits are different. ~ + Bitwise NOT: inverts all bits. << Left shift: shifts bits to the left (multiplies by 2). >> + Right shift: shifts bits to the right (divides by 2). // Program to demonstrate type casting in Java class TypeCastingExample { public static void main(String{] args) { ‘y 7 ee Introduction to Java Programming © long + 64-bit signed integer, used for very large values. mal values (single precision) © float ~ 32-bit floating-point, used for deci =e double + 64-bit floating-point, used for decimal values (double pee ey default for floating numbers). | @ char 16-bit Unicode character, stores a single character: boolean ~ represents only two values: true or false. 1.9.2 Non-Primitive (Reference) Data Types d by the fava; they are either create Non-primitive data types are not predefined in J Pe ‘yp a s, they store programmer or provided as built-in classes. Unlike primitive type’ — references (memory addresses) to objects. long Examples include : roe String ~ represents a sequence of characters (e.6, "Java")- © Arrays >a collection of elements of the same type. hile — 6 Classes and Objects > user-defined data types. cli. Interfaces ~> abstract types that define a contract for classes. it 1.10 Variables ( Avariable is a named storage location in a computer program that can hold data, * and its value can change during program execution. Example _intage = 11; Java is case-sensitive language. Stud and stud are different identifiers,

You might also like