经常在写Rdd的时候, 如:  val OWNER_ID=row.getAs("OWNER_ID")  等,

运行是可能会报异常 : java.lang.String cannot be cast to scala.runtime.Nothing

后检查后发现,其实应该保证类型一致,应该写成: val OWNER_ID=row.getAs[String]("OWNER_ID")

那么,问题来了, 为啥会有上面的异常出现。查询结果,觉得这篇文章有点意思,就摘抄过来了,

已做为学习,若侵犯您的版权,请在下评论@dy9776 , 我会尽快处理删除掉。

摘抄如下:

    1. Nothing是什么

      Nothing是一个类型(type)。就像Int、String一样是一个类型,比如值3是Int型的,值"abc"是String型的,与Int、String不同的是,不存在Nothing类型的值。

      Nothing这个类型(Type)还有个特点,他是Bottom Type,所谓Bottom type就是他是所有其他类型的子类型(Sub Type) (The bottom type is a subtype of all types.)。 所以可以认为Nothing是Int的子类型,Nothing也是String的子类型。下面摘自Scala Reference 3.5.2 Conformance

      For every value type T , scala.Nothing <: T <: scala.Any
      For every type constructor T (with any number of type parameters), scala.Nothing <: T <: scala.Any

    2. Nothing意义何在
      1. 意义之一:用于类型参数(covariant parameterized types)。
        首先,Scala的泛型是不允许不加类型参数的。 以下摘自Programming In Scala, 2nd Edition, Chapter 19 Type Parameterization

        Unlike Java, which allows raw types, Scala requires that you specify type parameters.
        (不像Java那样允许raw type,Scala必须指定类型参数)

        所以以下两行会出错:Error: type List takes type parameters

        type myList = List
        val li: List = List(1)

        改写成如下可以通过编译:

        type myList = List[String]
        val li: List[Int] = List(1)

        因为List(1)有一个Int型的元素:1,所以他的类型是List[Int]。嗯,make sense,一个List的类型取决于这个List里面的元素。所以List(“abc”)那么就是List[String]型,List(“abc”, 1)就是List[Any],因为"abc"和1都是Any型的子类型。类型推导就是这么工作的。

        那么List()呢?一个空的List,他是什么类型呢?肯定不是List[Int],因为里面并没有Int,也不是List[Any],因为里面啥都没有,没有理由推导出任何一个其他类型,于是这个List()只能是List[Nothing]。

        scala> val l = List()
        l: List[Nothing] = List()

        Nil就是继承自List[Nothing]的。所以才可以做1 :: Nil和"abc” :: Nil。也正因为Nil是List[Nothing],Nothing是bottome type,List是协变的,所以1 :: Nill才会被推导成List[Int],“abc” :: Nil会被推导成List[String]

        case object Nil extends List[Nothing] {
        override def isEmpty = true
        def head: Nothing =
        throw new NoSuchElementException("head of empty list")
        def tail: List[Nothing] =
        throw new NoSuchElementException("tail of empty list")
        }

        小实验,下面可以编译执行吗?ml1的类型是什么呢?

        val ml: List[String] = List()
        val ml1 = 2 :: ml

        意义之二,代表非正常退出。比如抛异常。这个也很make sense,异常嘛,肯定啥都返回不了,类型只有是Nothing了。

        scala> def e = throw new Exception("s")
        e: Nothing

        上文中的Nil的head方法也是一个例子。这里也体现了Nothing的Bottom Type的好处。

        scala> def er(p: Boolean) = if(p) 1 else throw new Exception("yeah")
        er: (p: Boolean)Int

        类型推导观察了下程序,发现这个er方法有2种可能的返回类型,一种是Int,一种是Nothing。由于Nothing是Bottom Type,所以Nothing是Int的子类,所以认为这2种可能返回的类型都是Int,所以推导出er方法的返回值是Int。

      说了这么多,Doc上就几句话:

      Nothing is - together with scala.Null - at the bottom of Scala's type hierarchy.

      Nothing is a subtype of every other type (including scala.Null); there exist no instances of this type. Although type Nothing is uninhabited, it is nevertheless useful in several ways. For instance, the Scala library defines a value scala.collection.immutable.Nil of type List[Nothing]. Because lists are covariant in Scala, this makes scala.collection.immutable.Nil an instance of List[T], for any element of type T.

      Another usage for Nothing is the return type for methods which never return normally. One example is method error in scala.sys, which always throws an exception.

      总结,Nothing是一个Bottom类型,不存在任何值属于Nothing类型。主要有2个用处,用于类型参数和代表非正常退出。

      参考资料:
      1: http://en.wikipedia.org/wiki/Bottom_type
      2: http://www.scala-lang.org/docu/files/ScalaReference.pdf
      3: http://www.scala-lang.org/api/2.10.4/index.html#scala.Nothing
      4: Programming In Scala, 2nd Edition

看完后,有点小明白了!

java.lang.String cannot be cast to scala.runtime.Nothing Scala中的Nothing类型的更多相关文章

  1. 记录maven java.lang.String cannot be cast to XX error

    在项目开发中自定义了一个maven plugin,在本地能够很好的工作,但是在ci server上却无法正常工作报错为: --------------------------------------- ...

  2. java.lang.ClassCastException: java.lang.String cannot be cast to com.jy.hfims.domain 映射实体类型错误

    今天在做 excel导出的时候,出现了一个问题"java.lang.ClassCastException: java.lang.String cannot be cast to com.do ...

  3. 解决jeesite开发java.lang.String cannot be cast to com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm$Principal问题

    解决jeesite问题java.lang.String cannot be cast to SystemAuthorizingRealm问题 这些天在jeesite项目上进行二次开发,遇到许多莫名其妙 ...

  4. Cause: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.ibatis.mapping.MappedStatement

    我用的是pagehelper 4.2.0,利用其进行表单的分页处理并进行展示,在第一次执行的时候能够看到分页后的结果,刷新一下第二次就显示不出来,控制台出现: Cause: java.lang.Cla ...

  5. ArrayMap java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]

    错误堆栈: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[] at android ...

  6. java.lang.String cannot be cast to java.util.Date

    我这个是个新建的功能,然后在保存的时候出现了这个错误.然后就找到了新建的action,发现其上的list方法出了问题. 这样是正确的.之前list<Constract>写成这样了.

  7. 【Java面试题】53 能不能自己写个类,也叫java.lang.String?

    可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...

  8. 能不能自己写个类,也叫java.lang.String?

    可以,但在应用的时候,需要用自己的类加载器去加载,否则,系统的类加载器永远只是去加载jre.jar包中的那个java.lang.String.由于在tomcat的web应用程序中,都是由webapp自 ...

  9. [Scala] java使用scala的jar包问题:Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Short

    场景 刚写的scala处理bmp文件的实验, 打了jar包让java调用一下, 结果发生这个错误. package org.tanglizi.bmp.demo; import org.tanglizi ...

随机推荐

  1. 奇怪吸引子---Arneodo

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  2. 卡卡游戏引擎之MVC模式下的事件处理

    前言 在前一篇文章 卡卡游戏引擎快速入门中提到了卡卡游戏引擎采用mvc的开发模式,这里相信介绍一下引擎在mvc模式下是如何做到低耦合的事件处理的. 在卡卡编辑器中选择一个节点,然后在左侧工具栏中切换到 ...

  3. go语言之进阶篇拷贝文件案例

    1.文件案例:拷贝文件 示例: package main import ( "fmt" "io" "os" ) func main() { ...

  4. 机器学习、NLP、Python和Math最好的150余个教程(建议收藏)

    编辑 | MingMing 尽管机器学习的历史可以追溯到1959年,但目前,这个领域正以前所未有的速度发展.最近,我一直在网上寻找关于机器学习和NLP各方面的好资源,为了帮助到和我有相同需求的人,我整 ...

  5. MFC中插入符的生成

    响应WM_CREATE消息的代码如下: int CTextView::OnCreate(LPCREATESTRUCT lpCreateStruct) { ) ; // 插入符在窗口重绘的过程中不会消失 ...

  6. Tushare数据的绘图操作

    1.在代码里调试学习实在费劲,可以把数据取到df里,在交互界面里慢慢调试 2.柱状图 绘制柱状图,默认情况下乱,数据太密了 改用曲线图

  7. jQuery 图片裁剪插件 Jcrop

    Jcrop是一个jQuery图片裁剪插件,它能为你的WEB应用程序快速简单地提供图片裁剪的功能.特点如下: 对所有图片均unobtrusively(无侵入的,保持DOM简洁) 支持宽高比例锁定 支持 ...

  8. 【算法】插入排序(Insertion Sort)

    (PS:内容参考MIT算法导论) 插入排序(Insertion Sort): 适用于数目较少的元素排序 伪代码(Pseudocode): 例子(Example): 符号(notation): 时间复杂 ...

  9. FastDFS_v4.06+nginx-1.4.2配置详解

    径不带group名(storage只有一个group的情况),如/M00/00/00/xxx:       location /M00 {            ngx_fastdfs_module; ...

  10. Selenium2(WebDriver)总结(二)---Firefox的firebug插件参数设置(补充)

    本文是对上一节的补充:http://www.cnblogs.com/puresoul/p/4251536.html 使用Selenium2(webdriver)启动firefox且自动加载firebu ...