Scala day7(Literals)

发布于:2025-05-20 ⋅ 阅读:(19) ⋅ 点赞:(0)

Foreword

We will continue to learn Scala knowledges, I believe in you already study many contents with yourself. I think Learning is one thing of yourself. Cheers!! my dear friends. Next, we start the travel about Scala.

Text

All Codes:

import language.deprecated.symbolLiterals
object day6 {
  def main(args:Array[String]): Unit = {
    System.out.println("Scala is make happy!!");
    Literal_num();
    Literal_char();
    System.out.println();
    Literal_Bool();
  }

  def Literal_num(): Unit = {
    val hex = 0x5
    val hex2 = 0x00FF
    val magic = 0xcafebabe
    System.out.println("hex = "+hex+"\nDataType:"+hex.getClass);
    System.out.println("hex2 = "+hex2+"\nDataType:"+hex2.getClass);
    System.out.println("magic = "+magic+"\nDataType:"+magic.getClass);
    System.out.println();
    val dec1 = 31;
    val dec2 = 255;
    val dec3 = 20;
    System.out.println("dec1 = "+dec1+"\nDataType:"+dec1.getClass);
    System.out.println("dec2 = "+dec2+"\nDataType:"+dec2.getClass);
    System.out.println("dec3 = "+dec3+"\nDataType:"+dec3.getClass);
    System.out.println();
    val prog = 0XCAFEBABEL;
    val tower = 35L;
    val lower_num = 243l;
    System.out.println("prog = "+prog+"\nDataType:"+prog.getClass);
    System.out.println("tower = "+tower+"\nDataType:"+tower.getClass);
    System.out.println("prog = "+lower_num+"\nDataType:"+lower_num.getClass);

    System.out.println();
    val num1:Short = 472;
    System.out.println("num1 = "+num1+"\nDataType:"+num1.getClass);
    val num2:Byte = 17;
    System.out.println("num2 = "+num2+"\nDataType:"+num2.getClass);
    System.out.println();

    val big = 1.2345;
    val bigger = 1.2345e1;
    val biggerStill = 123E45;

    System.out.println("big = "+big+"\nDataType:"+big.getClass);
    System.out.println("bigger = "+bigger+"\nDataType:"+bigger.getClass);
    System.out.println("biggerStill = "+biggerStill+"\nDataType:"+biggerStill.getClass);
    System.out.println();

    val F_num = 1.342F;
    val F_num2 = 3e5f;
    val F_num3 = 3e5;
    val F_num4 = 3e5D;

    System.out.println("F_num = "+F_num+"\nDataType:"+F_num.getClass);
    System.out.println("F_num2 = "+F_num2+"\nDataType:"+F_num2.getClass);
    System.out.println("F_num3 = "+F_num3+"\nDataType:"+F_num3.getClass);
    System.out.println("F_num4 = "+F_num4+"\nDataType:"+F_num4.getClass);
    System.out.println();
  }

  def Literal_char(): Unit = {
    val a = 'A';
    val d = "\u0041";
    val f = "\u0044";
    val Special_char = "\\";

    System.out.println("a = "+a+"\nDataType:"+a.getClass);
    System.out.println("d = "+d+"\nDataType:"+d.getClass);
    System.out.println("f = "+f+"\nDataType:"+f.getClass);
    System.out.println();
    System.out.println("Special_char = "+Special_char+"\nDataType:"+Special_char.getClass);

    val hello = "hello";
    System.out.println("hello = "+hello+"\nDataType:"+hello.getClass);
    val escapes = "\\\"\'";
    System.out.println("escapes = "+escapes+"\nDataType:"+escapes.getClass);
    System.out.println();

    System.out.println(
      """I like Java and
         Scala also like
         python,C,R,C++""");
    System.out.println();

    System.out.println(
    """
      I like Java and
      Scala also like
      python,C,R,C++
    """);

    System.out.println();

    System.out.println(
      """
        |I like Java and
        |Scala also like
        |python,C,R,C++
        |""".stripMargin);

    val char1 = 'OneSymbol;
    System.out.println("char1 = "+char1+"\nDataType:"+char1.getClass);
    val char_name = char1.name;
    System.out.println("char_name = "+char_name+"\nDataType:"+char_name.getClass);

  }

  def Literal_Bool(): Unit = {
    val bool1 = true;
    val bool2 = false;
    System.out.println("bool1 = "+bool1+"\nDataType:"+bool1);
    System.out.println("bool2 = "+bool2+"\nDataType:"+bool2);
  }

}

From the above program, we know the Literals have all kinds of type. It include Int literal, Long literal, Double literal, Float literal, character literal, String literal, Symbol literal, Bool literal.

Int literal like the below result:
在这里插入图片描述
The result explain this Int Type whatever assigned value, it will translate to default Decimal Int Type。

请添加图片描述
From this picture, we know if number start isn’t 0 and haven’t translate type, it will output default Decimal Int type.

请添加图片描述
The picture show result is Long Int type when number end with “L or l”。

请添加图片描述
The literal could Compulsorily change type, but the change value must in change data type area.

在这里插入图片描述
The Float data type, it default Double type, it must end with “F or f”。 Double Type literal could use “d or D” end to express. Through the picture from F_num2 to F_num4, the Double is default type, it also could obviuosly express.

在这里插入图片描述
From the above picture, we know char type is use single quation mark to express, String typeis use double quation mark to express. Because Scala is based on Java Virtual Machine designed. So it use Java most functions in libraries that lead to output result is it.

在这里插入图片描述
The picture show the special char and String output way.

在这里插入图片描述
Through the output result, we know directly ouput string isn’t ordered, it need use space full in the position, it also could use pipe character to full in the position and let it by ordered.

在这里插入图片描述
The Picture show Symbol Literal, it could gain name.

在这里插入图片描述
This picture show Boolean literal.

At Last

I know these contents are hard for Beginner, but we both like Big Data, as one Big Data Crazy fans, we must learn these knowledges. If you also like me, you need learn Python, Julia, C++, R, Paralleling Computing, Distributed Calculation Framework[Spark]. You still select to wait me write these passages, then you must wish my life have longer time that I could have more times to finish these contents.

Anything, you must remember one thing:" never give up your dream or give in life, you will become winner in this game when you insist to last. Cheers!! my dear friends, Your Pan.[One low-quality man of human]


网站公告

今日签到

点亮在社区的每一天
去签到