1 / 16

JAVA ile Programciliga Giris 1.2.2

Storing characters Harfleri Depolamak. Variables of type char store a single character code.char myCharacter = X';Character Escape Sequenceschar myCharacter = \u0058';System.out.println(\"It\'s freezing in here\", he said coldly.");\b Backspace\f Form feed<br> New line Carriage return\t

kyrie
Download Presentation

JAVA ile Programciliga Giris 1.2.2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. JAVA ile Programciliga Giris 1.2.2

    2. Storing characters Harfleri Depolamak Variables of type char store a single character code. char myCharacter = ‘X’; Character Escape Sequences char myCharacter = ‘\u0058’; System.out.println(“\”It\’s freezing in here\”, he said coldly.”); \b Backspace \f Form feed \n New line \r Carriage return \t Tab

    3. Character Arithmetic Harf aritmetigi Character arithmetic myCharacter += 1; // Increment to next character ++myCharacter; // Increment to next character char aChar = 0; char bChar = ‘\u0028’; aChar = (char)(2*bChar + 8); Here are the hexadecimal codes for the letters: A: 41 B: 42 C: 43

    4. Storing characters Harfleri Depolamak EXAMPLE- Arithmetic with Character Codes – CHARCODECALCS public class CharCodeCalcs { public static void main(String[] args){ char letter1 = ‘A’; // letter1 is ‘A’ char letter2 = (char)(letter1+1); // letter2 is ‘B’ char letter3 = letter2; // letter3 is also ‘B’ System.out.println(“Here\’s a sequence of letters: “+ letter1 + letter2 + (++letter3)); // letter3 is now ‘C’ System.out.println(“Here are the decimal codes for the letters:\n”+ letter1 + “: “ + (int)letter1 + “ “ + letter2 + “: “ + (int)letter2 + “ “ + letter3 + “: “ + (int)letter3); } } import static java.lang.Integer.toHexString;

    5. Bitwise Operations Bit Islemleri & AND , | OR ^ Exclusive OR if both bits are the same the result is 0; otherwise, the result is 1. ~ Complement it inverts all the bits, so that each 1 bit becomes 0, and each 0 bit becomes 1 Using the AND OR Operators thirdBit = indicators & 0x4; // Select the 3rd bit

    6. Bitwise Operations Bit Islemleri EXAMPLE- Bitwise AND and OR operations-BITWISEOPS import static java.lang.Integer.toBinaryString; public class BitwiseOps { public static void main(String[] args) { int indicators = 0xFF07; int selectBit3 = 0x4; // Mask to select the 3rd bit // Try the bitwise AND to select the third bit in indicators System.out.println(“indicators = “ + toBinaryString(indicators)); System.out.println(“selectBit3 = “ + toBinaryString(selectBit3)); indicators &= selectBit3; System.out.println(“indicators & selectBit3 = “ + toBinaryString(indicators)); // Try the bitwise OR to switch the third bit on indicators = 0xFF09; System.out.println(“\nindicators = “+ toBinaryString(indicators)); System.out.println(“selectBit3 = “+ toBinaryString(selectBit3)); indicators |= selectBit3; System.out.println(“indicators | selectBit3 = “ + toBinaryString(indicators)); // Now switch the third bit off again indicators &= ~selectBit3; System.out.println(“\nThe third bit in the previous value of indicators” + “ has been switched off”); System.out.println(“indicators & ~selectBit3 = “ + toBinaryString(indicators)); } }

    7. Bitwise Operations Bit Islemleri Using the Exclusive OR operator a ^= b; byte allBitsOne = 0xFF; // Wrong!! byte allBitsOne = 0xFFFFFFFF; // Correct – well done!! Shift Operations << Shift left, filling with zeros from the right. >> Shift right, propagating the sign bit from the left. >>> Shift right, filling with zeros from the left.

    8. Bitwise Operations Bit Islemleri EXAMPLE-Using shift Operations-PACKING CHARACTERS import static java.lang.Long.toHexString; public class PackingCharacters { public static void main(String[] args) { char letterA = ‘A’; char letterB = ‘B’; char letterC = ‘C’; char letterD = ‘D’; long packed = 0L; packed = letterD; // Store D packed = (packed << 16) | letterC; // Shift and add the next letter - C packed = (packed << 16) | letterB; // Shift and add the next letter - B packed = (packed << 16) | letterA; // Shift and add the next letter - A System.out.println(“packed now contains 0x” + toHexString(packed)); // Now unpack the letters and output them long mask = 0xFFFF; // Rightmost 16 bits as 1 char letter = (char)(packed & mask); // Extract the rightmost letter System.out.println(“From right to left the letters in packed are:”); System.out.println(“ “ + letter + “ 0x” + toHexString(letter)); packed >>= 16; // Shift out the rightmost letter letter = (char)(packed & mask); // Extract the new rightmost letter System.out.println(“ “ + letter + “ 0x” + toHexString(letter)); packed >>= 16; // Shift out the rightmost letter letter = (char)(packed & mask); // Extract the new rightmost letter System.out.println(“ “ + letter + “ 0x” + toHexString(letter)); packed >>= 16; // Shift out the rightmost letter letter = (char)(packed & mask); // Extract the new rightmost letter System.out.println(“ “ + letter + “ 0x” + toHexString(letter)); } }

    9. Bitwise Operations Bit Islemleri Methods for Bitwise operations bitCount(arg) highestOneBit(arg) lowestOneBit(arg) numberOfLeadingZeros(arg) int data = 0x0F00; // data is: 0000 0000 0000 0000 0000 1111 0000 0000 int bits = Integer.bitCount(data); // Result is 4

    10. Bitwise Operations EXAMPLE- Methods for Bitwise operations-TRYBITMETHODS import static java.lang.Long.*; public class TryBitMethods { public static void main(String[] args) { long number = 0xF00000000000000FL; System.out.println(“number:\n” + toBinaryString(number)); long result = rotateLeft(number,2); System.out.println(“number rotated left 2 bits:\n” + toBinaryString(result)); result = rotateRight(number, 3); System.out.println(“number rotated right 3 bits:\n” + toBinaryString(result)); result = reverse(result); System.out.println(“Previous result reversed:\n” + toBinaryString(result)); System.out.println(“Bit count in number:\n” + bitCount(number)); } } 76

    11. enumeration Variables with a fixed set of integer values You will often need variables that can have values only from a predefined fixed set. enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Day weekday = Day.Tuesday;

    12. enumeration Variables with a fixed set of integer values enum Month { January, February, March , April , May , June, July , August , September, October, November, December } Month current = Month.September; // Initialize to September current = Month.October;

    13. enumeration EXAMPLE-Using enumeration- TRYENUMERATION public class TryEnumeration { // Define an enumeration type for days of the week enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } public static void main(String[] args) { // Define three variables of type Day Day yesterday = Day.Thursday; Day today = Day.Friday; Day tomorrow = Day.Saturday; // Output the values of the Day variables System.out.println(“Today is “ + today); System.out.println(“Tomorrow will be “ + tomorrow); System.out.println(“Yesterday was “ + yesterday); } }

    14. Boolean variables Bool Degiskenleri Variables of type boolean can have only one of two values, true or false. The values true and false are boolean literals boolean state = true; state = false;

    15. Operator precedence Islem Önceligi a = b + c + 10; a = (b + c) + 10; (), [], postfix ++, postfix -- unary +, unary -, prefix ++, prefix --, ~, ! (type), new *, /, % +, - <<, >>, >>> < ,<= , >, >=, instanceof ==, != & ^ | && || =, +=, -=, *=, /=, %=, <<=, >>=, >>>=, &=, |=, ^=

    16. comments Yorumlar Program comments /*************************************** * This is a long explanation of * * some particularly important * * aspect of program operation. * ***************************************/ Documentation Comments /** This is a documentation comment. */ //single line comment

More Related