Download as pdf
Download as pdf
You are on page 1of 7
Ve area eerMan Ly a) Hindamentats of Java Progen nan Javiciy a ory paps ith hove proyneaaniningy bana snyontor apyaitonny son ay lataluaye ylicattts, doskte apypicatiane, We anh sina antiry” A Tay employ instead of transaing Java code ta machine Langa was). When the bytecode (alsa syytisains il WG hhly apinnised ser at insta Salted a favaebisy flo 4s 44 bo Am on a veOMMPUROE, a Fava interpreter, called the Java Vietwal Machine Init Tins heen used widely to create various types OF ‘ob based applications, mobile ato Hones aL MY AE FAYE jevode, it (IVD. gansfates the hytevente anes actin cove and then executes tt Piygrammer hay compited a Jaya program into b Vine fant wf AnH AW AMIN a anew sou Bo ania ay atta cv Widows, Hint, oF Mite) as tone as ithas «JVM ronning on it Java prs plathoras dependent anal highly pORUAD aie inate Dane Types and Varlabtes: Variation: Vesti tho paratatin hita wo WHE se vartables, A variable is a placeholder for data that can change its Jotal_marks and ince they can have Vaile staniayy prnygeam eNecupion, Jaye porwontage vatoutator program, wo will ase three variables named marks obtai ed of type double, ANwontayo, The vattiablos marks obtained andl perwentage are deca aetiantal values Hoxtting point numbers), Inside the muain method, we declare these variables, we also assign town alloy asin Hho = aporator ay shown belo UNE Gaal NAHAS = 40; ctouute maths obvatinod = 346; shaulo powentage = 005 Dooateuate the powentage pervontage = anarks obtained/totaLmarks)*100; Vy shyplay the porventge in the IDE output window "Student's Percentage = "+percentage); Systemout print short, float, double, Boolean, char. Deimnitive Data Types Jy all, Javat supports eight primitive data types, they are byte, int, long, 2. Variable names can begin with cither an alphabetic character, an underscore (_), or a dollar sign ($). However, convention is to begin a variable name with a letter. They can consist of only alphabets, digits, and underscore, 2. Variable names must be one word, Spaces are not allowed in variable names. Underscores are allowed. & There ate some reserved words in Java that cannot be used as variable names, for example - int. 4. Jaya is a case-sensitive language. & You can define multiple variables of the same type in one statement by separating each with ¢ comms, to make variable names meaningful. The name should indicate the use of that variable. S.ltis good prsetice we want variables to store textual data, for example, the name of @ student. racter, we use the String class in Java, Eg. String first_name Mayank’ ‘Suring Variables: To store more than one chat Control Flow: Is a sequence of instructions. Java executes the instructions in sequential order, that is, one after the other. Selection Structures In real life, you often select your actions based 0 ide, you carry an umbrella, otherwise not. fe a lets us execute a if Else Statement : The if statement in Jav: ssion evaluates to tue or false. wn whether a condition is tue or false. For example. if itis block of code depending upon whether en e ‘Switch Statement: Gass Nos Clase: Xit ‘Tople: Unit-3: Subject: Fundamentals of Jaya Programming. jntormatio vology (80: Javaisa very popular high level programming language and has been used widely to create various types of ‘computer applications such as database applications, desktop applications, Web based applications, mobile applications, and games aniong others. A Java compiler instead of translating Java code to machine language cole, translates i into Java Rytecode (a highly optimized set of instructions). When the bytecode called a Java class file) is to be rum on a computer, a Java interpreter, called the Java Virtual Machine (JVM). translates the bytecode into machine code and then executes it, ‘The advantage of such an approach is that once a programmer has compiled a Java program into bytecode, it can be nun on any platform (say Windows, Linux, or Mac) as tong as it has a JVM running on it, This makes Java programs platform independent and highly portable. Data Types and Variables: Variabl To store the program data we will use variables. A variable is a placcholder for data that can change its, value during program execution, In our percentage calculator program, we will use three variables named marks_obtained,total_marks and percentage. The variables marks_oblained and percentage are declared of type double, since they can have fractional values (floating point numbers). Inside the main method, we declare these variables, we also assign them values using the = operator as shown below: int otal_marks = 400; double marks_obtained = 346; double percentage = 0.0; To calculate the percentage percentage = (marks_obtained/total_marks)*100; To display the percentage in the IDE output window ‘System.out.printin("'Student1's Percentage = "-+percentage); Primitive Data Types In all, Java supports cight primitive data types, they are byte, int, long, shor, float, double, Boolean, char. 1. Variable names can begin with either an alphabetic character, an underscore (_), ora dollar sign (8). However, convention is to begin a variable name with a letter. They can consist of only alphabets, digits, and underscore. 2. Variable names must be one word. Spaces are not allowed in variable names. Underscores are allowed, 3, There are some reserved words in Java that cannot be used as variable names, for example - int 4, Java is a case-sensitive language. : : 5, Its good practice to make variable names meaningful, The name should indicate the use ofthat variable. 6. You can define multiple variables of the same type in one statement by separating each with a comma. String Variables: we want variables to store textual data, for example, the name of a student. To store more than one character, we use the String class in Java, Eg. String first_name = "Mayank"; Control Flow: Is a sequence of instructions, Java executes the instructions in sequential order, that is, one after the other. Selection Structures Jn real life, you often select your actions based on whether a condition is true or false. For example, if itis raining outside, you carry an umbrella, otherwise not. ‘The if Else Statement : The if statement in Java lets us execute a block of code depending upon whether an expression evaluates to true or false, ‘The Switch Statement: N wossible values, 5 Stetement ¥ ysed to execute a hock of eee matching one value out of many p ‘ture of the J, ava switch statement is as follows: pression) | cee coneIARL_T : statemente; break case cxmnstant_2 : statements; weak; ; Sefault: statements: break: Repetition Structures, The ability of a comy of statements d ci m. mes goes through three iterations. loop that repeats the execution of the body three ti Java provides three statements ‘ on ~the while statement, the do while statement. andthe for statement for ‘The for Statement The for loop is the most widely used Jav: 'a loop construct. TI T he structure of the Java for statement for (counter=initial_value: oe eee test_condition;change counter) Statements 1 System.outprintin(*" + i); Vhile Statement The while statement evaluates the test before executi controlled loop. The structure of the Java while state ‘The While Statement while (expression) { statements ing the body of a loop. A while loop is an entry ment is as shown: Example: WAP to print first § natural numbers using WHILE loop i=0; hile (is=5) { ‘System.out.printin(" +i); iets 1 Do While Statement o The do while statement evaluates the test after executing the body of a loop. A do-while loop is an exit control loop. j The structure of the Java do while statement is as shown: do { statements, } while (expression); Example: WAP to print first 5 natural numbers using do while loop inti Systemout printin(™ + i; int ] while (1 <=5); ARRAY ‘An array is a collection of similar types of data, Arrays are used to store multiple values a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets {J ‘Types of Array in java ‘There are two types of array. Single Dimensional Array Multidimensional Array Single Dimensional Array in Java Syntax to Declare an Array in Java 1, dataTypef) ar; (oF) 2. dataType ar); Instantiation of an Array in Java 1 arrayRefVar=new datatypelsize);, For example, if we want to store the names of 100 people then we can crcate an array of the string type that cean store 100 names, String(] array = new String{100]; Here, the above array cannot store more than 100 names, The number of values in a Java array is always fixed. How to declare an array in Java? Tn Java, here is how we can declare an array. dataTypel] arrayName; dataType - it can be primitive date types like int, char, double, byte, et. or Java objects arrayName - itis an identifier For example: double(] data; Here, data is an array that can hold values of type double. Access the Elements of an Array: Example public static void main(String!) args) ( String|] cars = {"Volvo", "BMW", "Ford, "Mazda"; System. out prinln(cars{0)); 1 ] Output; Volvo Note: Array indexes start with 0: (0] isthe first element. [1] is the second element, etc. Change an Array Element To change the value ofa specific element, we have to refer to the index number: cars{0} = "Opel"; Example: Public static void main(Stringt} args) { String] cars = ("Volvo", "BMW", "Ford?, “Marda"); cars(0} = "Opel", \Sysem ul printin(cars{0}), Ourput: Oped Array Length To Find out how Example: Public static void main(String{} args) { String} cars = ("Volvo", "BMW", "Ford’, "Mazda"; System.out.printin(cars.length); ‘many clements an array has, use the length property: Ouput: 4 Loop Through an Array Using loop through the array elements withthe for loop, and use the Jength property to specify how many times the loop should run, Example: i Public static void main(Stringt) args) { String{] cars = {"Volvo", "BMW", "For for (inti = 05: < cars.length; i#4) { ‘System.out printin(cars{i}); Example: Public static void main(String args{]) { int a[}=new int{5); O:finitialization 0; af2]=10; a[3]=40; al4]=50; for(int i=0;i= 40) assed"; System.out.print((i+1)+" System out print(marks(i)+"\ System.outprint(percentage+ Wt"); System.out printia(result); 1 Example: WAP to sort an array of Strings in alphabetic order: ) public static void main(String argst) { String|] names = (*Shruti", "Kunal", "Gungun",”Avani","Ravi","Trpti","Purvs ‘System.out.printin("Names Array before Sorting:"); j

You might also like